TL; DR今天什么是使用ASP.NET Core 2.0设置HTTPS的正确方法?为HTTPS配置ASP.NET Core 2.0 Kestrel
我想将我的项目配置为使用https和证书,如他们在BUILD 2017上显示的那样。我尝试了几个设置,但没有任何工作。经过一番调查,我更加困惑。似乎有很多方法来配置URL和端口...我已经看到appsettings.json
,hosting.json
,通过代码,并在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
}
}
}
}
}
}
但它始终把URL和launchsettings.json
端口,当我在命令行开始dotnet run
或当我开始从Visual Studio调试器。
这是我Program.cs
和Startup.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" });
});
}
}
正如我刚才所说,我是不是能够得到它在任何工作constallation。今天什么是使用ASP.NET Core 2.0设置HTTPS的正确方法?
在生产中,最好使用Nginx作为前端,使用nginx配置SSL/TLS非常简单,您可以保持kestrel为http服务。其众所周知的做法是为后端执行ssl卸载,以避免不必要的复杂性和性能降低,而不会产生安全影响。 MS Azure应用类似的架构师概念。 –