2017-09-21 302 views
9

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

我想将我的项目配置为使用https和证书,如他们在BUILD 2017上显示的那样。我尝试了几个设置,但没有任何工作。经过一番调查,我更加困惑。似乎有很多方法来配置URL和端口...我已经看到appsettings.jsonhosting.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.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" }); 
     }); 
    } 
} 

正如我刚才所说,我是不是能够得到它在任何工作constallation。今天什么是使用ASP.NET Core 2.0设置HTTPS的正确方法?

+1

在生产中,最好使用Nginx作为前端,使用nginx配置SSL/TLS非常简单,您可以保持kestrel为http服务。其众所周知的做法是为后端执行ssl卸载,以避免不必要的复杂性和性能降低,而不会产生安全影响。 MS Azure应用类似的架构师概念。 –

回答

15

不幸的是,在启动ASP.NET Core 2.0之前,已经在各种视频或教程中显示的基于配置的HTTPS设置方式并未进入最终版本。

2.0,配置HTTPS的唯一途径是在代码中,通过明确建立红隼听众,作为解释in this announcement,并使用ListenOptions.UseHttps启用HTTPS:

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

不幸的是,在发布时间,官方文档没有正确覆盖这一点,并宣传了未实现的基于配置的方式。自那以后,这一点已经被修复

从ASP.NET Core 2.1开始,基于配置的HTTPS设置将成为可能,如最初所承诺的那样。这可能会是这样的,由Tratcher on GitHub解释说:

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

到目前为止,我还没有收到有关什么可行,什么不可行,或者为什么不更新文档的官方确认以反映现状。您可以阅读关于此主题on GitHub的更多信息。


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

.UseKestrel(options => 
{ 
    // listen for HTTP 
    options.Listen(IPAddress.Parse("127.0.0.1"), 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.Listen(IPAddress.Parse("127.0.0.1"), 40001, listenOptions => 
      { 
       listenOptions.UseHttps(certificate); 
      }); 
     } 
    } 
}) 
+0

谢谢,现在我知道我为什么如此困惑......你能否在你的示例中添加一些代码,它反映了我在https的“appsettings.json”中所做的设置?我真的很感激。 – VSDekar

+0

添加(未经测试)应与您的配置相匹配的代码。 – poke

相关问题