Jun*_*r M 6 c# https ssl-certificate kestrel-http-server asp.net-core-webapi
我已经使用在Stackoverflow上找到的建议使用powershell生成了证书:
New-SelfSignedCertificate -Subject "CN=Test Code Signing" -Type CodeSigningCert -KeySpec "Signature" -KeyUsage "DigitalSignature" -FriendlyName "Test Code Signing" -NotAfter (get-date).AddYears(5)
Run Code Online (Sandbox Code Playgroud)
我已将此证书复制并粘贴到“受信任的根证书颁发机构”中。
我的NET Core WebAPI Program.cs设置如下:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options=> {
options.Listen(IPAddress.Loopback, 5000); // http:localhost:5000
options.Listen(IPAddress.Any, 80); // http:*:80
options.Listen(IPAddress.Loopback, 443, listenOptions =>
{
//how to use a certificate store here?
//listenOptions.UseHttps("certificate.pfx", "password");
//listenOptions.UseHttps(StoreName.My, "Test Code Signing", allowInvalid: true);
listenOptions.UseHttps(StoreName.My, "localhost", allowInvalid: true);
});
});
Run Code Online (Sandbox Code Playgroud)
无论是本地主机或测试代码签名在这个代码工作,因为他们无法找到。也许我缺少了一些东西。试图遵循此MSDN文档,但没有运气。
目前,Chrome浏览器上显示的证书与我在个人和受信任的根证书颁发机构中拥有的证书不同:
如何设置Kestrel以选择浏览器信任的自签名证书,并避免阻止诸如的消息NET::ERR_CERT_AUTHORITY_INVALID?
小智 2
您正在使用的 UseHttps 重载不允许您指定存储位置,因此它默认为 StoreLocation.CurrentUser。您需要调用一个从存储中检索证书并将其传递给 UseHttps 方法的方法。有一篇 MSDN 文章提供了更多详细信息,我已将其包含在底部,但这里有一个示例(您需要将“此处的通用名称”替换为证书通用名称):
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel(options =>
{
options.Listen(IPAddress.Any, 443, listenOptions =>
{
listenOptions.UseHttps(GetHttpsCertificateFromStore());
listenOptions.NoDelay = true;
});
})
.Build();
}
private static X509Certificate2 GetHttpsCertificateFromStore()
{
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=[your common name here]", false);
if (currentCerts.Count == 0)
{
throw new Exception("Https certificate is not found.");
}
return currentCerts[0];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1274 次 |
| 最近记录: |