简体   繁体   中英

client certificate is not coming through

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

在此处输入图片说明

Your client certificate is not trusted because the chain of trust fails and as a result it is rejected. I believe the southworks.com link you posted is making the assumption that the client and server code are running on the same box. If you are using a separate server for your server code, it will not be able to validate your client certificate unless you install your self-made CA cert (DevelopmentCA.cer) into the server's Trusted Root Certification Authorities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM