【问题标题】:.NET Core adding client certificate to POST to Kestrel REST API on Linux fails server cert validation.NET Core 在 Linux 上将客户端证书添加到 POST 到 Kestrel REST API 失败服务器证书验证
【发布时间】:2019-10-14 05:43:58
【问题描述】:

我在 Linux 上使用 .NET Core 2.1,Kestrel。

我的 Web 应用程序作为客户端发出请求(按照周围点缀的指南,这似乎是要走的路):

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
X509Certificate2 cert = GetClientCertificate();
handler.ClientCertificates.Add(cert);

using (var client = new HttpClient(handler))
     {
        var myRequest= new myRequest()
          {
                foo = bar,
            };

var response = await client.PostAsync(myUrl, myRequest, new JsonMediaTypeFormatter());

我已将 Kestrel 配置为:

    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Listen(IPAddress.Any, 443, listenOptions =>
            {

            var httpsConnectionAdapterOptions = new HttpsConnectionAdapterOptions()
                {
                    ClientCertificateMode = ClientCertificateMode.RequireCertificate,
                    SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
                    ServerCertificate = GetSSLCertificate(),
                    ClientCertificateValidation = CertificateValidator.MyCustomerValidatorForLogging
                };
                listenOptions.UseHttps(httpsConnectionAdapterOptions);
            });
        }
        )
    .Build();

我添加了一个自定义验证器(只是为了看看发生了什么,看起来像这样):

public static bool MyCustomerValidatorForLogging(X509Certificate2 certificate, X509Chain chain, SslPolicyErrors errors)
        {
            Log.Info("Received Request");
            Log.Error(errors.ToString());

            if (errors == SslPolicyErrors.None)
            {
                return true;
            }

            return false;
        }

GetClientCertificate() 是中间CA签署的客户端认证SSL证书。

GetSSLCertificate() 是用于标准服务器身份验证 SSL 证书的证书。

我已将客户端身份验证证书的颁发 Sub CA 和 CA 证书复制到 /usr/local/share/ca-certificates/(“存储”)并发出“update-ca-certificates”命令。我相信正是这些证书用于验证客户端证书。

服务器收到请求时,errors值为: “RemoteCertificateChainErrors”并拒绝请求。

请问大家有什么想法吗?

【问题讨论】:

  • 这意味着验证 SSL 证书时出现错误 RemoveCertificateChainErrors 是一个包含错误的数组。当 RemoteCertificateChainErrors 发生时,您可以检索 ChainStatus 并获取详细信息。

标签: c# authentication .net-core certificate kestrel


【解决方案1】:

对于带有 Apache 服务器的 Linux ubuntu 操作系统,我们也遇到了同样的问题。 我们通过附加 SSL 证书解决了这个问题。使用 Apache 配置。

【讨论】:

  • 我没有使用 Apache :(
【解决方案2】:

事实证明这确实有效!我只需要获得正确的证书!

具体来说,我必须在服务器端包含根 CA 和中间 CA。
客户端,我只需要包含根 CA 来验证。

【讨论】:

  • 你是什么意思,你必须在 cilent 端包含根 CA?您是否还必须将根 CA 证书附加到客户端请求?我遇到了类似的事情……
  • 因为我的客户端运行的是 Linux,所以我必须添加到信任中,即根 CA - 它会发送一个带有根公钥的请求,而不仅仅是叶。如果您正在运行 MS Windows 并且请求来自此,那么您必须确保 CA 具有根证书。
猜你喜欢
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 2011-03-17
  • 2018-05-27
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多