ASP.NET Konfigurieren Kestrel, um HTTPS aus Windows Certificate Store zu verwenden

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: ASP.NET Konfigurieren Kestrel, um HTTPS aus Windows Certificate Store zu verwenden

by Anonymous » 14 Jul 2025, 14:37

Ich versuche, HTTPS für Kestrel mithilfe von Zertifikatstore zu konfigurieren. Hier ist, was ich bisher erreicht habe:
Appsettings.json

Code: Select all

{
"SSLCertificate": {
"Serial": "serialNumberFromCertificateStore"
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "https://*:8090",
"Protocols": "Http1"
},
"gRPC": {
"Url": "https://*:8091",
"Protocols": "Http2"
}
}
}
}
program.cs
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?

Top