0

使用未來:.NET 4.0,VS2010和webapi1.0 我跟着這個鏈接http://southworks.com/blog/2014/06/16/enabling-ssl-client-certificates-in-asp-net-web-api/客戶證書沒有被通過

強制客戶端發送證書驗證

在服務器端的代碼如下所示低於

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()總是返回null我是否缺少其他設置?不知道爲什麼客戶端證書沒有通過?

客戶端的代碼看起來像下面

 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(); 
      } 

      } 

更新:

我嘗試使用小提琴手,並通過它調用GETRESPONSE 和這裏的代碼調試,這就是我回來

enter image description here

+0

可能是相同的問題http://stackoverflow.com/questions/22817965/httprequestmessage-getclientcertificate-returns-null-in-web-api - 遺憾的是沒有公認的答案有作爲。 – Lucero

+0

我會嘗試使用小提琴手攔截和分析SSL流量,以查看是否客戶端證書實際上是發送到服務器端與否。此外,您必須確保(例如使用MMC)客戶端使用帶有私鑰的證書,只是公共證書部分不起作用。 – Lucero

+0

@ Lucero:如何確保這兩個部分都用於MMC?我所指的ClientCertificatesTest是否有錯?在MMC添加/刪除管理單元中添加 – Gauls

回答

0

您的客戶端證書不可信,因爲信任鏈失敗並因此重新鏈接遭離棄。我相信你發佈的southworks.com鏈接假設客戶端和服務器代碼在同一個框上運行。如果你正在使用你的服務器代碼單獨的服務器,除非您安裝自制CA證書(DevelopmentCA.cer)到服務器的受信任的根證書頒發機構就無法驗證客戶端證書。

相關問題