gpt4 book ai didi

asp.net - 具有客户端证书身份验证的 .Net Core Web API

转载 作者:行者123 更新时间:2023-12-04 12:16:12 24 4
gpt4 key购买 nike

我在 .Net Core 2.1 中开发了一个简单的 WEB API 服务

我正在尝试实现客户端证书身份验证,因此我只能向在其计算机上安装了特定证书的客户端授予对 API 的访问权限。

客户端使用浏览器(Chrome、Edge、IE11 或 Firefox)访问 API。

我在 API 方法中添加了对证书的请求:

[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{

X509Certificate2 cert = Request.HttpContext.Connection.ClientCertificate;
if (cert!=null && cert.Verify())
{
//more verification here...
return Content("....", "application/json");
}
else
{
return Content("....", "application/json");
}

}

然后我安装了一个自签名证书并添加到受信任的根,启用客户端身份验证目的。

enter image description here

但是变量 cert 始终为空,并且浏览器在我请求页面时甚至没有提示我使用证书。

我想是因为我必须在某个地方设置 Web 服务器必须要求客户端证书的位置,因为可以在 IIS 中设置,但是在我的开发环境中,我使用的是 IIS Express。

如何强制 IIS express 请求客户端证书?

最佳答案

要使用 ASP.NET Core 身份验证堆栈进行正确的证书身份验证,您还可以查看 idunno.Authentication.Certificate来自 Barry Dorrans他自己。它允许您为应用程序启用证书身份验证并像处理任何其他身份验证方案一样处理它,因此您可以将实际的基于证书的逻辑排除在业务逻辑之外。

This project sort of contains an implementation of Certificate Authentication for ASP.NET Core. Certificate authentication happens at the TLS level, long before it ever gets to ASP.NET Core, so, more accurately this is an authentication handler that validates the certificate and then gives you an event where you can resolve that certificate to a ClaimsPrincipal.

You must configure your host for certificate authentication, be it IIS, Kestrel, Azure Web Applications or whatever else you're using.



请务必查看 “documentation”关于如何正确设置它,因为它需要 configuration of the host正常工作,就像使用 IIS Express 一样。包括原始 Kestrel、IIS、Azure 或一般反向代理等其他服务器的说明。

关于asp.net - 具有客户端证书身份验证的 .Net Core Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com