【发布时间】: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