Appsettings.json
Code: Select all
{
"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>
Ich habe die Seriennummer von AppStings.json gelesen und nach dem Abrufen des Zertifikats in der USHTTPS -Methode angewendet. Das Problem ist, dass es versucht, Port 8090 zweimal zu hören. Wenn ich den Port ändere, hört er diesen Port an und erkennt das Zertifikat an, antwortet jedoch nicht mit irgendetwas.
Was soll ich hier tun?