gpt4 book ai didi

c# - HttpRequestMessage.GetClientCertificate() 在 Web API 中返回 null

转载 作者:太空狗 更新时间:2023-10-29 19:47:00 29 4
gpt4 key购买 nike

我有一个 .NET4.5 WebAPI 2 应用程序,它使用 SSL 客户端证书进行一些自定义安全相关检查。

在调试应用程序时,request.GetClientCertificate() 为任何服务调用返回 null,无论我目前尝试了什么。

测试代码:

基本上,在我的服务端,我有一个类似这样的方法:

class CertificateChecker : ICertificateChecker
{
public bool Check(HttpRequestMessage request)
{
var cert = request.GetClientCertificate();
}
}

我使用 request.Properties.Add(HttpPropertyKeys.ClientCertificateKey, cert) 使用附加到请求的客户端证书进行单元测试通过(证书完全符合预期,验证有效,等等).

但是,当我使用调用实际 WebAPI 应用程序的 HttpClient 进行测试时,断点设置在服务端的 request.GetClientCertificate() 行,同一行返回 null。

这是我尝试过的:

客户端证书:

  • 创建了一个假的 CA 并将其放入受信任的 CA 存储区(尝试了 LocalMachine 和当前用户)。
  • 创建了由该假 CA 签名的客户端身份验证 (1.3.6.1.5.5.7.3.2) 证书并将其放入个人存储(本地计算机和当前用户)。

(基本上,关注了https://www.piotrwalat.net/client-certificate-authentication-in-asp-net-web-api-and-windows-store-apps/和类似的博客)

在我使用 HttpClient 调用服务的测试中,通过以下方式附加了客户端证书(均无效):

  • 使用 WebRequestHandler,将 ClientCertificateOptions 设置为 Manual,并将证书添加到 ClientCertificates 集合。

    using (var handler = new WebRequestHandler())
    {
    var cert = GetTestCert();
    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
    handler.ClientCertificates.Add(cert);

    // and so on...
    }
  • 使用 HttpRequestMessage,将证书添加为属性,并将键设置为 HttpPropertyKeys.ClientCertificateKey(此方法适用于如上所述的单元测试)。

    request.Properties.Add(HttpPropertyKeys.ClientCertificateKey, cert);

在这两种情况下,cert 变量都设置为预期的 X509Certificate2 对象。

托管:

  • IISExpress:尝试在 IISExpress 中运行应用。

    编辑 applicationhost.config 并启用 iisClientCertificateMappingAuthentication 设置为“true”和以下访问设置(均无效):

    <access sslFlags="Ssl, SslNegotiateCert" />
    <access sslFlags="SslNegotiateCert" />
  • 本地 IIS将 Web 应用程序设置为在本地计算机上的 IIS 中运行

    • IIS 中的站点配置了使用可信证书的 HTTPS 绑定(bind)。
    • Web 应用配置为使用 https 协议(protocol)并启用 IIS 客户端证书映射身份验证。
    • 尝试了两种访问选项组合(Ssl、SslNegotiateCertSslNegotiateCert)(通过配置编辑器)

在这两种情况下,当使用网络浏览器通过 https url 访问网络 api 时,我可以毫无问题地显示家庭 Controller 的索引 View (因此,服务器端证书是可信的)。

最佳答案

我知道它有点旧,但我遇到了同样的问题,我通过关注这篇文章解决了它:

https://improveandrepeat.com/2017/07/how-to-configure-iis-express-to-accept-ssl-client-certificates/

问题是 IIS 服务器上的配置,也可以在 IIS express 上修改。我报告它以防链接不可用。

For IIS change the server configuration file, for IIS Express modify the file under your solution located in .vs\config\applicationhost.config

Search for an xml element called inside a element:

<security>
<access sslFlags="None" />

The default configuration has no support for SSL client certificates. You need to modify the sslFlags attribute to include these options: Ssl, SslNegotiateCert, SslRequireCert

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

The next step is to find the element :

<iisClientCertificateMappingAuthentication enabled="false"></iisClientCertificateMappingAuthentication>

If you change enabled to true, IIS Express will start accepting client certificates:

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

You now need to restart Visual Studio and IIS Express. IIS Express can be restarted using the icon in your system tray.

From now on you will be asked for a client certificate and you can debug the whole application inside Visual Studio. This may not look like a big improvement, but trust me, it makes debugging much simpler.

希望这可以帮助其他人:-)

关于c# - HttpRequestMessage.GetClientCertificate() 在 Web API 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817965/

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