using : .net 4.0, VS2010 and webapi1.0 I followed this link http://southworks.com/blog/2014/06/16/enabling-ssl-client-certificates-in-asp-net-web-api/
to enforce clients to send certificate to Authenticate
On the server side the code looks like below
public class RequireCertificateFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var request = actionContext.Request;
if (!this.AuthorizeRequest(request.GetClientCertificate()))
{
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
}
private bool AuthorizeRequest(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2)
{
bool result = false;
if (x509Certificate2 != null)
{
string issuer = x509Certificate2.Issuer;
string subject = x509Certificate2.Subject;
result = true;
}
return result;
}
request.GetClientCertificate() Always return null am i missing any other settings? not sure why client certificate is not coming through?
Client code looks like below
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "ClientCertificatesTest", true)[0];
// Build HTTP Request
HttpWebRequest wrStatus = (HttpWebRequest)WebRequest.Create("https://localhost/TestAPI/api/Home");
wrStatus.KeepAlive = true;
wrStatus.Method = WebRequestMethods.Http.Get;
wrStatus.Accept = "text/xml";
wrStatus.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)";
wrStatus.ClientCertificates.Clear();
wrStatus.ClientCertificates.Add(cert);
string result = null;
using (HttpWebResponse resp = (HttpWebResponse)wrStatus.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
}
Update :
I tried using fiddler and debugged through the code where it calls getResponse and here's what i get back