Kestrel - 使用我的机器上已有的特定 SSL 证书

发布于 2025-01-11 01:47:03 字数 1589 浏览 0 评论 0原文

Optimizely CMS(艺术家以前称为EPiServer)最近发布了.Net Core版本。我可以使用 Kestrel 运行我的网站。但是,我想为我的网站设置一个特定的 url,并且我想为此 url 使用现有的 SSL 证书。

该证书安装在我的计算机上的 WebHosting 商店中。

这是我的 Kestrel 配置:

launchSettings.json

"MySampleProject": {
  "commandName": "Project",
  "launchBrowser": true,
  "externalUrlConfiguration": true,
  "applicationUrl": "https://sampleproject.local.hostname.dev",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

appsettings.json

"Kestrel": {
"Endpoints": {
  "HttpsInlineCertStore": {
    "Url": "https://sampleproject.local.hostname.dev",
    "Certificate": {
      "Subject": "local.hostname.dev",
      "Store": "WebHosting",
      "Location": "LocalMachine",
      "AllowInvalid": "true"
    }
  }
} 

在 program.cs 中

public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
    {
        
            return Host.CreateDefaultBuilder(args)
                .ConfigureCmsDefaults()
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false);
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                });
        }
    

我认为配置存在一些问题?但我很难找到有关如何执行此操作的文档。

Optimizely CMS (the artist formerly known as EPiServer) recently released a .Net Core version. I can run my site using Kestrel. But, I want to set a specific url for my site, and I want to use an already existing SSL cert for this url.

The cert is installed on my machine in the WebHosting store.

Here is my Kestrel config:

launchSettings.json

"MySampleProject": {
  "commandName": "Project",
  "launchBrowser": true,
  "externalUrlConfiguration": true,
  "applicationUrl": "https://sampleproject.local.hostname.dev",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

appsettings.json

"Kestrel": {
"Endpoints": {
  "HttpsInlineCertStore": {
    "Url": "https://sampleproject.local.hostname.dev",
    "Certificate": {
      "Subject": "local.hostname.dev",
      "Store": "WebHosting",
      "Location": "LocalMachine",
      "AllowInvalid": "true"
    }
  }
} 

In program.cs

public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
    {
        
            return Host.CreateDefaultBuilder(args)
                .ConfigureCmsDefaults()
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false);
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                });
        }
    

I assume there is some issue with the config? But I am having a hard time finding documentation around how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2025-01-18 01:47:03

宾果游戏。找到了所需的方法来做到这一点。

这是appsettings

"Kestrel": {
  "Endpoints": {
    "Https": {
      "Url": "https://sampleproject.local.hostname.dev:8001",
      "Certificate": {
        "Subject": "local.hostname.dev",
        "Store": "webhosting",
        "Location": "LocalMachine"
      }
    }
  }
}

然后是Program.cs

return Host.CreateDefaultBuilder(args)
                .ConfigureCmsDefaults()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseKestrel();
                    webBuilder.UseStartup<Startup>();
                });

Bingo. Found the desired way to do it.

Here is the appsettings

"Kestrel": {
  "Endpoints": {
    "Https": {
      "Url": "https://sampleproject.local.hostname.dev:8001",
      "Certificate": {
        "Subject": "local.hostname.dev",
        "Store": "webhosting",
        "Location": "LocalMachine"
      }
    }
  }
}

And then in Program.cs

return Host.CreateDefaultBuilder(args)
                .ConfigureCmsDefaults()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseKestrel();
                    webBuilder.UseStartup<Startup>();
                });
王权女流氓 2025-01-18 01:47:03

这是“一个”解决方案。我不认为这是最好的解决方案,但它确实有效。

这是我的 Kestrel 应用程序设置:

"Kestrel": {
"Endpoints": {
  "HttpsInlineCertStore": {
    "Url": "https://sampleproject.local.hostname.dev",
    "Certificate": {
      "Subject": "local.hostname.dev",
      "Store": "webhosting",
      "Location": "LocalMachine",
      "AllowInvalid": "false"
    }
  }
}

}

然后在我的 Program.cs 中,

var storeName = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Store"];
            var storeLocationString = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Location"];
            var storeLocation = StoreLocation.LocalMachine;
            if (storeLocationString == "CurrentUser")
            {
                storeLocation = StoreLocation.CurrentUser;
            }
            var subject = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Subject"];
            var port = 8001;

            services.Configure<KestrelServerOptions>(options =>
            {
                options.Listen(IPAddress.Any, port, listenOptions =>
                {
                    // Enable support for HTTP1 and HTTP2 (required if you want to host gRPC endpoints)
                    listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                    // Configure Kestrel to use a certificate from a local .PFX file for hosting HTTPS
                    listenOptions.UseHttps(CertificateLoader.LoadFromStoreCert(subject, storeName, storeLocation, false), configureOptions: _ => { });
                });
            });

关键是如何指向不在默认证书存储中的证书。我管理多个网站,我的证书位于该网络托管商店中。有一个带有商店名称的枚举,但虚拟主机不是该枚举的一部分。而且,我希望它在配置中,而不是代码中,因此不同的开发人员可以有不同的设置。

我想让它只使用应用程序设置中的内容,我只需调用 UseKestral() 或其他内容并从配置中读取。但这至少有效。

This is "a" solution. I dont think its the best solution, but it works.

Here is my appsettings for Kestrel:

"Kestrel": {
"Endpoints": {
  "HttpsInlineCertStore": {
    "Url": "https://sampleproject.local.hostname.dev",
    "Certificate": {
      "Subject": "local.hostname.dev",
      "Store": "webhosting",
      "Location": "LocalMachine",
      "AllowInvalid": "false"
    }
  }
}

}

Then in my Program.cs

var storeName = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Store"];
            var storeLocationString = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Location"];
            var storeLocation = StoreLocation.LocalMachine;
            if (storeLocationString == "CurrentUser")
            {
                storeLocation = StoreLocation.CurrentUser;
            }
            var subject = context.Configuration["Kestrel:Endpoints:HttpsInlineCertStore:Certificate:Subject"];
            var port = 8001;

            services.Configure<KestrelServerOptions>(options =>
            {
                options.Listen(IPAddress.Any, port, listenOptions =>
                {
                    // Enable support for HTTP1 and HTTP2 (required if you want to host gRPC endpoints)
                    listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                    // Configure Kestrel to use a certificate from a local .PFX file for hosting HTTPS
                    listenOptions.UseHttps(CertificateLoader.LoadFromStoreCert(subject, storeName, storeLocation, false), configureOptions: _ => { });
                });
            });

The keys thing was how do I point to a cert that wasnt in the default cert store. I manage several sites, and my cert is in that webhosting store. There is an Enum with store names, but webhosting isnt part of that enum. AND, I wanted it in config, not code, so a different developer could have a different set up.

I would like to make it so that it just uses whats in the appsettings and I just call UseKestral() or something and reads from the config. But this at least works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文