asp.net - IIS 是否可以要求 SSL 客户端证书而不将它们映射到 Windows 用户?

标签 asp.net authentication iis ssl client-certificates

我希望能够将 SSL 客户端证书映射到 ASP.NET Identity 用户。我希望 IIS 完成尽可能多的工作(协商客户端证书并可能验证它是否由受信任的 CA 签名),但我不希望 IIS 将证书映射到 Windows 用户。客户端证书被传递到 ASP.NET,在那里它被检查并映射到 ASP.NET Identity 用户,后者被转换成 ClaimsPrincipal。

到目前为止,我能够让 IIS 将客户端证书传递给 ASP.NET 的唯一方法是启用 iisClientCertificateMappingAuthentication 并设置到 Windows 的多对一映射帐户(然后再也不会用于任何其他用途。)有没有任何方法可以让 IIS 在没有此配置步骤的情况下协商并传递证书?

最佳答案

您不必使用 iisClientCertificateMappingAuthentication。客户端证书可在 HttpContext 中访问。

var clientCert = HttpContext.Request.ClientCertificate;

要么在整个网站上启用 RequireClientCertificate,要么使用单独的 login-with-clientcertificate 页面。

下面是在 ASP.NET MVC 中执行此操作的一种方法。希望您可以使用其中的一部分来满足您的具体情况。

  1. 首先确保您可以通过启用功能委托(delegate)在 web.config 中设置 SslFlags。

IIS - Feature delegation

  1. 使站点接受(但不要求)客户端证书 enter image description here

  2. 设置需要客户端证书的 login-with-clientcertificate-page 路径。在这种情况下,用户 Controller 具有 CertificateSignin 操作。 web.config

  3. 创建一个登录 Controller (伪代码)

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    [AllowAnonymous()]
    public ActionResult CertificateSignIn()
    {
        //Get certificate
        var clientCert = HttpContext.Request.ClientCertificate;
    
        //Validate certificate
        if (!clientCert.IsPresent || !clientCert.IsValid)
        {
            ViewBag.LoginFailedMessage = "The client certificate was not present or did not pass validation";
            return View("Index");
        }
    
        //Call your "custom" ClientCertificate --> User mapping method.
        string userId;
        bool myCertificateMappingParsingResult = Helper.MyCertificateMapping(clientCert, out userId);
    
        if (!myCertificateMappingParsingResult)
        {
            ViewBag.LoginFailedMessage = "Your client certificate did not map correctly";
        }
        else
        {
            //Use custom Membersip provider. Password is not needed!
            if (Membership.ValidateUser(userId, null))
            {
                //Create authentication ticket
                FormsAuthentication.SetAuthCookie(userId, false);
                Response.Redirect("~/");
            }
            else
            {
                ViewBag.LoginFailedMessage = "Login failed!";
            }
        }
    
        return View("Index");
    }
    

关于asp.net - IIS 是否可以要求 SSL 客户端证书而不将它们映射到 Windows 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040512/

相关文章:

c# - 从代码隐藏 (C#) 添加 JavaScript 引用

.net - .net 2.0 中的 System.DirectoryServices.AccountManagement

amazon-web-services - 如何打印 aws-requests-auth 发送的规范字符串?

powershell - 如何使用 PowerShell 在 IIS 中设置 rapidFailProtectionInterval?

asp.net - 无法访问 IIS 元数据库 ASP.Net

asp.net - 如何更改 swagger 文档基本网址?

asp.net - 如何使用 CSS 重新定位此图像?

ruby - 与 sinatra 的 Authlogic?

php - Laravel API 护照 - 无法登录

iis - IIS 7 中 mp3 mimeMap 的奇怪行为