gpt4 book ai didi

c# - 为 HTTPS 配置 ASP.NET Core 2.0 Kestrel

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:22 24 4
gpt4 key购买 nike

TL;DR 今天使用 ASP.NET Core 2.0 设置 HTTPS 的正确方法是什么?

我想将我的项目配置为使用 https 和他们在 BUILD 2017 中显示的证书.我尝试了几种设置,但没有任何效果。经过一番研究,我更加困惑了。似乎有很多方法可以配置 URL 和端口......我已经看到 appsettings.jsonhosting.json,via code,以及 launchsettings.json 我们还可以设置 URL 和端口。

是否有“标准”方法来做到这一点?

这是我的appsettings.development.json

{
"Kestrel": {
"Endpoints": {
"Localhost": {
"Address": "127.0.0.1",
"Port": "40000"
},
"LocalhostWithHttps": {
"Address": "127.0.0.1",
"Port": "40001",
"Certificate": {
"HTTPS": {
"Source": "Store",
"StoreLocation": "LocalMachine",
"StoreName": "My",
"Subject": "CN=localhost",
"AllowInvalid": true
}
}
}
}
}
}

但当我使用 dotnet run 从命令行启动时,或者当我从 Visual Studio 使用调试器启动时,它总是从 launchsettings.json 获取 url 和端口.

这是我的 Program.csStartup.cs

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}

public class Startup
{
public IConfiguration Configuration { get; }
public string Authority { get; set; } = "Authority";
public string ClientId { get; set; } = "ClientId";

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.Configure<MvcOptions>(options => options.Filters.Add(new RequireHttpsAttribute()));

JsonConvert.DefaultSettings = () => new JsonSerializerSettings() {
NullValueHandling = NullValueHandling.Ignore
};

services.AddSingleton<IRepository, AzureSqlRepository>(x => new AzureSqlRepository(Configuration.GetConnectionString("DefaultConnection")));
services.AddSingleton<ISearchSplitService, SearchSplitService>();

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => new JwtBearerOptions {
Authority = this.Authority,
Audience = this.ClientId
});

services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions() { HotModuleReplacement = true, ReactHotModuleReplacement = true, HotModuleReplacementEndpoint = "/dist/__webpack_hmr" });
}

app.UseStaticFiles();
app.UseAuthentication();

app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{id?}");

routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
}

正如我所说,我无法让它在任何星座中工作。今天使用 ASP.NET Core 2.0 设置 HTTPS 的正确方法是什么?

最佳答案

遗憾的是,在 ASP.NET Core 2.0 发布之前的各种视频或教程中展示的基于配置的 HTTPS 设置方式并没有进入最终版本。

对于 2.0,配置 HTTPS 的唯一方法是在代码中,通过显式设置 Kestrel 监听器,如解释的那样 in this announcement ,并使用 ListenOptions.UseHttps启用 HTTPS:

var host = new WebHostBuilder()
.UseKestrel(options =>
{
options.ListenAnyIP(443, listenOptions =>
{
listenOptions.UseHttps("server.pfx", "password");
});
})
.UseStartup<Startup>()
.Build();

不幸的是,在发布时,官方文档没有正确涵盖这一点,并宣传了未实现的基于配置的方式。 This has been fixed since.

从 ASP.NET Core 2.1 开始,基于配置的 HTTPS 设置将成为可能,正如最初 promise 的那样。这可能看起来像这样,如 Tratcher on GitHub 所解释的那样:

"Kestrel": {
"Endpoints": {
"HTTPS": {
"Url": "https://*:443",
"Certificate": {
"Path": "server.pfx",
"Password": "password"
}
}
}
}

在您的特定示例中,基于代码的配置如下所示。请注意,如果您不想使用证书文件,您需要先从证书存储中手动检索证书。

.UseKestrel(options =>
{
// listen for HTTP
options.ListenLocalhost(40000);

// retrieve certificate from store
using (var store = new X509Store(StoreName.My))
{
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectName,
"localhost", false);
if (certs.Count > 0)
{
var certificate = certs[0];

// listen for HTTPS
options.ListenLocalhost(40001, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
}
}
})

关于c# - 为 HTTPS 配置 ASP.NET Core 2.0 Kestrel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com