Applettings.json
Код: Выделить всё
{
"SSLCertificate": {
"Serial": "serialNumberFromCertificateStore"
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "https://*:8090",
"Protocols": "Http1"
},
"gRPC": {
"Url": "https://*:8091",
"Protocols": "Http2"
}
}
}
}
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
var kestrelSection = context.Configuration.GetSection("Kestrel");
var certSerial = context.Configuration.GetSection("SSLCertificate").GetValue("Serial");
if (!string.IsNullOrEmpty(certSerial))
{
// Retrieve the certificate from the Windows certificate store
using var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certificate = store.Certificates.Where(f => f.SerialNumber.ToUpper().Equals(certSerial.ToUpper())).FirstOrDefault();
if (certificate != null)
{
serverOptions.Configure(kestrelSection);
// Configure HTTPS endpoint with the retrieved certificate
serverOptions.ListenAnyIP(8090, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
}
}
});
< /code>
Я прочитал серийный номер сертификата от Appsettings.json и примените его в методе usehttps после получения сертификата. Проблема в том, что он пытается начать слушать порт 8090 дважды. Если я меняю порт, он слушает этот порт и распознает сертификат, но не отвечает ни с чем.
Что я должен здесь делать?
Подробнее здесь: https://stackoverflow.com/questions/779 ... cate-store