我希望能够将 SSL 客户端证书映射到 ASP.NET Identity 用户。我希望 IIS 完成尽可能多的工作(协商客户端证书并可能验证它是否由受信任的 CA 签名),但我不希望 IIS 将证书映射到 Windows 用户。客户端证书传递到 ASP.NET,在 ASP.NET 中对其进行检查并映射到 ASP.NET 身份用户,该用户将转换为 ClaimsPrincipal。
到目前为止,我能够让 IIS 将客户端证书传递到 ASP.NET 的唯一方法是启用iisClientCertificateMappingAuthentication
并设置到 Windows 帐户的多对一映射(该帐户随后不会用于其他任何用途。)是否有任何方法可以让 IIS 协商并传递证书,而无需执行此配置步骤?
您不必使用 iisClientCertificateMappingAuthentication。客户端证书可在 HttpContext 中访问。
var clientCert = HttpContext.Request.ClientCertificate;
您可以在整个站点上启用 RequireClientCertificate 或使用单独的使用客户端证书登录 page.
下面是在 ASP.NET MVC 中执行此操作的一种方法。希望您可以使用其中的部分内容来适合您的具体情况。
- 首先确保您可以通过打开功能委派来在 web.config 中设置 SslFlags。
![IIS - Feature delegation](https://i.stack.imgur.com/wqhMl.png)
Make site accept (but not require) Client Certificates
![enter image description here](https://i.stack.imgur.com/l4sal.png)
Set path to login-with-clientcertificate-page where client certificates will be required. In this case a User controller with a CertificateSignin action.
![web.config](https://i.stack.imgur.com/COLCZ.png)
-
创建登录控制器(伪代码)
[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");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)