简体   繁体   English

带有客户端证书身份验证的.Net Core Web API

[英].Net Core Web API with Client Certificate Authentication

I've developed a simple WEB API service in .Net Core 2.1 我在.Net Core 2.1中开发了一个简单的WEB API服务

I'm trying to implement a client certificate authentication, so I can give access to the APIs only to the clients that have a specific certificate installed on their machine. 我正在尝试实现客户端证书身份验证,因此我只能将API访问其计算机上安装了特定证书的客户端。

The clients access the API using a browser (Chrome, Edge, IE11 or Firefox). 客户使用浏览器(Chrome,Edge,IE11或Firefox)访问API。

I've added in the API method the request for the certificate: 我在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");
    }

}

then I've installed a self-signed certificate and added to the Trusted Root, enabling the Client Authentication purpose. 然后我安装了一个自签名证书并添加到受信任的根目录,启用了客户端身份验证。

在此输入图像描述

but the variable cert is always null and the browser didn't even prompt me to use a certificate when I request the page. 但变量cert始终为null,当我请求页面时,浏览器甚至没有提示我使用证书。

I suppose because I have to set somewhere that the web server must ask for the client certificate as it is possible to set in IIS, but in my development environment, I'm using IIS Express. 我想因为我必须在某处设置Web服务器必须要求客户端证书,因为可以在IIS中设置,但在我的开发环境中,我使用的是IIS Express。

How can I force IIS express to request a client certificate? 如何强制IIS express请求客户端证书?

For proper certificate authentication using the ASP.NET Core authentication stack, you can also check out idunno.Authentication.Certificate by Barry Dorrans himself. 要使用ASP.NET Core身份验证堆栈进行正确的证书身份验证,您还可以查看Barry Dorrans自己的idunno.Authentication.Certificate It allows you to enable certificate authentication for your application and handles it like any other authentication scheme, so you can keep actual certificate-based logic out of your business logic. 它允许您为应用程序启用证书身份验证,并像处理任何其他身份验证方案一样处理它,因此您可以将实际的基于证书的逻辑保留在业务逻辑之外。

This project sort of contains an implementation of Certificate Authentication for ASP.NET Core. 此项目包含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. 证书身份验证在TLS级别发生,早在它到达ASP.NET Core之前,因此,更准确地说,这是一个验证证书的身份验证处理程序,然后为您提供一个事件,您可以将该证书解析为ClaimsPrincipal。

You must configure your host for certificate authentication, be it IIS, Kestrel, Azure Web Applications or whatever else you're using. 您必须配置主机以进行证书身份验证,无论是IIS,Kestrel,Azure Web应用程序还是您正在使用的任何其他内容。

Make sure to also check out the “documentation” on how to set this up properly, since it requires configuration of the host to work properly, just like you did with IIS Express. 请务必查看有关如何正确设置此文档“文档” ,因为它需要配置主机才能正常工作,就像使用IIS Express一样。 Instructions for other servers like raw Kestrel, IIS, Azure or general reverse proxies are included. 包括其他服务器的说明,如原始Kestrel,IIS,Azure或一般反向代理。

In order to enable IIS Express to start requesting client certificates and therefore pass them to the server side, the configuration file must be edited: 为了使IIS Express能够开始请求客户端证书并因此将它们传递到服务器端,必须编辑配置文件:

The whole configuration is inside the solution folder in the .vs\\config\\applicationhost.config 整个配置位于.vs \\ config \\ applicationhost.config中的解决方案文件夹中

Ensure the following values are set: 确保设置以下值:

<security>
   <access sslFlags="Ssl, SslNegotiateCert, SslRequireCert" />

and

<iisClientCertificateMappingAuthentication enabled="true"></iisClientCertificateMappingAuthentication>

For local testing, you can enable SSL in IIS Express from Visual Studio. 对于本地测试,您可以从Visual Studio中在IIS Express中启用SSL。 In the Properties window, set SSL Enabled to True. 在“属性”窗口中,将“SSL Enabled”设置为“True”。 Note the value of SSL URL; 注意SSL URL的值; use this URL for testing HTTPS connections. 使用此URL来测试HTTPS连接。

For Who needs Details here 对于谁需要详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET 核心 Web API 身份验证和 .NET 身份 - .NET Core Web API Authentication and .NET Identity 本地UWP客户端到本地asp.net核心api Web服务器-证书颁发机构无效或不正确 - Local UWP client to local asp.net core api web server - The certificate authority is invalid or incorrect ASP.NET Core Web API 身份验证 - ASP.NET Core Web API Authentication 在 C# 中使用 web API 与客户端证书身份验证 - Consume web API with client certificate authentication in C# ASP.net 核心 API 和 React 客户端的 Azure 身份验证 - Azure authentication for ASP.net core API and React client 如何在Web API 2(ASP.Net Core)中实现基本身份验证? - How to implement basic authentication in web API 2 (ASP.Net Core)? ASP.NET Core 1.0 Web API 中的简单 JWT 身份验证 - Simple JWT authentication in ASP.NET Core 1.0 Web API ASP.NET 内核 Web API 谷歌认证 - ASP.NET Core Web API Google Authentication 处理身份验证/授权:ASP.NET Core Web 应用程序 =&gt; ASP.NET Core Web API =&gt; SQL - Handling authentication/authorization: ASP.NET Core Web Application => ASP.NET Core Web API => SQL Asp.net 核心客户端证书身份验证中间件在 X509Chain 构建上失败 - Asp.net core Client certificate authentication middleware failing on X509Chain build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM