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

enb*_*081 25 asp.net authentication digital-certificate

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

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

我如何在asp.net c#中实现这一目标?

Kna*_*ģis 23

要创建安全的身份验证机制,您将使用客户端证书和用户名/密码.原因是证书是可以被盗(复制)的东西,但密码是只有人知道的东西.替代方案可以是智能卡上的证书,受PIN保护.

要在ASP.NET应用程序中使用客户端证书,您需要执行以下操作:

步骤1:在IIS管理器中,打开您的应用程序或网站,选择"SSL设置",然后选择"需要SSL"和"要求客户端证书".

现在,当用户打开您的网站时,浏览器将提示他选择将在通信中使用的客户端证书.

重要信息此时,您必须确保证书是由您信任的人颁发的(因为任何人都可以创建自己的自签名证书).

第2步:添加配置项(web.config,数据库等).在此列表中,您将为客户端证书添加整个CA(证书颁发机构)链的指纹.

<add key="ClientCertificateIssuerThumbprints" value="4901f5b87d736cd88792bd5ef7caee91bf7d1a2b,0113e31aa85d7fb02740a1257f8bfa534fb8549e,c9321de6b5a82666cf6971a18a56f2d3a8675602"/>
Run Code Online (Sandbox Code Playgroud)

第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.");
Run Code Online (Sandbox Code Playgroud)

只有在检查了密码和证书时,才允许用户进入系统.


flu*_*lup 11

假设您具有IIS 7.0或更高版本,则可以配置客户端证书映射身份验证

使用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>
Run Code Online (Sandbox Code Playgroud)

或者使用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>
Run Code Online (Sandbox Code Playgroud)

(样本配置相当无耻地从iis.net文档页面上的示例中复制,非常复杂.)

或者,您可以将应用程序配置为使用基于声明的身份验证和安全令牌服务(STS),该服务根据客户端证书对客户端进行身份验证.ADFS 2.0可以完全填充此角色,或者如果它不可用,您可以查看Thinktecture Identity Server.