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

标签 asp.net asp.net-core certificate

我在 .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/

相关文章:

c# - 从 appSettings.json 获取值(value)?

c# - Asp.Net Core 2.0 Web 应用程序静态文件给出 404

certificate - 使用 certreq 工具创建证书,其中我使用 openssl 生成了 csr

ssl - 如何禁用 CXF 3.0.0+ jax-rs 2.0 客户端的 CN 检查

angular - 我已经为我的后端 ASP.NET Core Web API 为本地主机创建了一个 SSL/TLS 证书,我可以与我的前端 Angular 应用程序共享它吗?

c# - Fiddler - asp.net web api 自定义 POST

javascript - 应用某些 css 样式后,fileupload click 事件未正确触发(在 IE 8 上)

asp.net - C# Web API 模型绑定(bind)器提供程序应该如何工作?

c# - 正则表达式接受部分匹配

c# - 无法在面向 netcoreapp3.0 的 ASP.NET Core WebApp 中为特定 API Controller 启用 CORS