通过asp.net中的X509证书进行客户端身份验证

问题描述:

我有一个asp.net应用程序,我需要使用X509证书对用户进行身份验证。也就是说,用户必须安装由我发布的证书,以便他可以浏览我的网站,并且可以通过此证书识别哪个用户是。通过asp.net中的X509证书进行客户端身份验证

我已经在IIS上配置SSL,但这不是我现在正在寻找的,我不知道从哪里开始。

如何在asp.net c#中实现这个功能?

要创建安全身份验证机制,您可以同时使用客户端证书和用户名/密码。原因是证书是可以被盗取(复制)的东西,但是密码是仅由该人员知道的。替代方案可以是智能卡上的证书,由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

假设您有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