我一直在尝试使用XCA生成的证书通过asp.net核心设置2路TLS。
首先,我创建了一个根证书。
然后我生成了服务器证书:
然后我生成了客户端证书:
设置服务器Kestrel:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.ConfigureHttpsDefaults(confOptions => {
confOptions.ClientCertificateMode = Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.RequireCertificate;
confOptions.CheckCertificateRevocation = true;
confOptions.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
confOptions.ServerCertificate = new X509Certificate2("servercertificate.p12", "password");
}
);
options.Listen(IPAddress.Loopback, 5001, listenOptions =>
{
listenOptions.UseHttps();
});
})
.UseStartup<Startup>();
}
使用其他程序中的HttpClient:
var cert = new X509Certificate2("clientcertificate.p12", "password");
var _clientHandler = new HttpClientHandler();
_clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
_clientHandler.ClientCertificates.Add(cert);
_clientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
HttpClient client = new HttpClient(_clientHandler);
client.BaseAddress = new System.Uri("https://127.0.0.1:5001");
var result = "";
try {
result = client.GetStringAsync("api/values").GetAwaiter().GetResult();
} catch (Exception e) {
Console.WriteLine(e.InnerException);
}
当客户端连接时,服务器拒绝连接。服务器将以下内容转储到控制台中:
Failed to authenticate HTTPS connection.
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
如果我禁用了服务器上的客户端证书检查,则将根公共证书添加到证书存储中,并将浏览器指向127.0.0.1,服务器证书似乎有效。
我是否缺少证书的某些配置,或者这与我的代码错误有关?