Can IIS require SSL client certificates without mapping them to a windows user?

会有一股神秘感。 提交于 2019-12-01 10:43:48

You do not have to use the iisClientCertificateMappingAuthentication. The client certificate is accessible in the HttpContext.

var clientCert = HttpContext.Request.ClientCertificate;

Either you enable RequireClientCertificate on the complete site or use a separate login-with-clientcertificate page.

Below is one way of doing this in ASP.NET MVC. Hopefully you can use parts of it to fit your exact situation.

  1. First make sure you are allowed to set the SslFlags in web.config by turning on feature delegation.

  1. Make site accept (but not require) Client Certificates

  2. Set path to login-with-clientcertificate-page where client certificates will be required. In this case a User controller with a CertificateSignin action.

  3. Create a login controller (pseudo-code)

    [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");
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!