2013-02-18 97 views
21

我有一個asp.net應用程序,我需要使用X509證書對用戶進行身份驗證。也就是說,用戶必須安裝由我發佈的證書,以便他可以瀏覽我的網站,並且可以通過此證書識別哪個用戶是。通過asp.net中的X509證書進行客戶端身份驗證

我已經在IIS上配置SSL,但這不是我現在正在尋找的,我不知道從哪裏開始。

如何在asp.net c#中實現這個功能?

回答

20

要創建安全身份驗證機制,您可以同時使用客戶端證書和用戶名/密碼。原因是證書是可以被盜取(複製)的東西,但是密碼是僅由該人員知道的。替代方案可以是智能卡上的證書,由PIN保護。

要使用ASP.NET應用程序的客戶端證書,你需要做到以下幾點:

步驟1:在IIS管理器,打開應用程序或網站,選擇SSL設置,選擇都需要SSL和要求客戶證書。

現在,當用戶打開您的網站時,瀏覽器會提示他選擇將用於通信的客戶端證書。

重要此時,您必須確保證書由您信任的人員簽發(因爲任何人都可以創建自己的自簽名證書)。

第2步:添加一個配置項(web.config,數據庫等)。在此列表中,您將爲客戶端證書添加整個CA(證書頒發機構)鏈的縮略圖。

<add key="ClientCertificateIssuerThumbprints" value="4901f5b87d736cd88792bd5ef7caee91bf7d1a2b,0113e31aa85d7fb02740a1257f8bfa534fb8549e,c9321de6b5a82666cf6971a18a56f2d3a8675602"/> 

步驟3:創建一個經典的用戶名/密碼登錄頁面。驗證用戶名/密碼。

第4步:下面的代碼添加到您的登錄頁面:

var x509 = new X509Certificate2(this.Request.ClientCertificate.Certificate); 
var chain = new X509Chain(true); 
chain.ChainPolicy.RevocationMode = X509RevocationMode.Offline; 
chain.Build(x509); 

var validThumbprints = new HashSet<string>(
    System.Configuration.ConfigurationManager.AppSettings["ClientCertificateIssuerThumbprints"] 
     .Replace(" ", "").Split(',', ';'), 
    StringComparer.OrdinalIgnoreCase); 

// if the certificate is self-signed, verify itself. 
for (int i = chain.ChainElements.Count > 1 ? 1 : 0; i < chain.ChainElements.Count; i++) 
{ 
    if (!validThumbprints.Contains(chain.ChainElements[i].Certificate.Thumbprint)) 
     throw new UnauthorizedAccessException("The client certificate selected is not authorized for this system. Please restart the browser and pick the certificate issued by XXXXX"); 
} 

// certificate Subject would contain some identifier of the user (an ID number, SIN number or anything else unique). here it is assumed that it contains the login name and nothing else 
if (!string.Equals("CN=" + login, x509.Subject, StringComparison.OrdinalIgnoreCase)) 
    throw new UnauthorizedAccessException("The client certificate selected is authorized for another user. Please restart the browser and pick another certificate."); 

只有當這兩個密碼和證書已檢查,用戶應在系統允許的。

+0

如果客戶端可以將其導出並安裝到任何他想要的位置,證書的要點是什麼?用戶名/密碼可以確保用戶的真實性,但我還需要確保機器的真實性。 – enb081 2013-03-06 07:46:46

+0

可以安裝證書,以便無法導出私鑰(用於身份驗證)。一些筆記本電腦允許您將證書安裝在硬件芯片中。另一種方法是將證書存儲在智能卡中。 – 2013-03-06 08:01:22

+1

如果您找到足夠的客戶端證書,則使用它進行驗證:http://www.iis.net/configreference/system.webserver/security/authentication/iisclientcertificatemappingauthentication – flup 2013-03-09 18:22:38

8

假設您有IIS 7.0或更高版本,可以通過配置客戶端證書映射身份驗證

Using Active Directory(非常容易,出圖工作AD服務器)

<location path="Default Web Site"> 
    <system.webServer> 
     <security> 
     <access sslFlags="Ssl, SslNegotiateCert" /> 
      <authentication> 
      <windowsAuthentication enabled="false" /> 
      <anonymousAuthentication enabled="false" /> 
      <digestAuthentication enabled="false" /> 
      <basicAuthentication enabled="false" /> 
      <clientCertificateMappingAuthentication enabled="true" /> 
     </authentication> 
    </security> 
    </system.webServer> 
</location> 

或者using IIS(更多配置所需在IIS中,需要訪問客戶端證書,但獨立工作,不會往返AD)。在這種情況下,您可以指定(一個或多個)用戶憑證和

  • 地圖中的每個用戶證書的公鑰其憑據您指定的用戶或
  • 基於價值觀給用戶多個證書地圖證書的領域

配置(多對一):

<location path="Default Web Site"> 
    <system.webServer> 
     <security> 
     <authentication> 
      <windowsAuthentication enabled="false" /> 
      <anonymousAuthentication enabled="false" /> 
      <digestAuthentication enabled="false" /> 
      <basicAuthentication enabled="false" /> 
      <iisClientCertificateMappingAuthentication enabled="true" 
        manyToOneCertificateMappingsEnabled="true"> 
       <manyToOneMappings> 
        <add name="Contoso Employees" 
         enabled="true" 
         permissionMode="Allow" 
         userName="Username" 
         password="[enc:AesProvider:57686f6120447564652c2049495320526f636b73:enc]"> 
        <rules> 
         <add certificateField="Subject" 
          certificateSubField="O" 
          matchCriteria="Contoso" 
          compareCaseSensitive="true" /> 
        </rules> 
        </add> 
       </manyToOneMappings> 
      </iisClientCertificateMappingAuthentication> 
     </authentication> 
     <access sslFlags="Ssl, SslNegotiateCert" /> 
     </security> 
    </system.webServer> 
</location> 

(示例配置而無恥地從樣本複製的iis.net做這是非常複雜的。)

或者您可以使用安全令牌服務(STS)將應用程序配置爲use Claims-Based Authentication,該服務根據客戶端證書對客戶端進行身份驗證。 ADFS 2.0可以充分利用這個角色,或者如果它不可用,您可以查看Thinktecture Identity Server

相關問題