0

В приведенном ниже методе основного ядра ASP.NET, как я могу определить среду хостинга, поэтому я могу переключаться между различными файлами сертификатов для HTTPS?Определение среды хостинга при настройке Kestrel и UseHttps

public sealed class Program 
{ 
    public static void Main(string[] args) 
    { 
     new WebHostBuilder() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseKestrel(
       options => 
       { 
        if ([Development Hosting Environment]) 
        { 
         options.UseHttps("DevelopmentCertificate.pfx"); 
        } 
        else 
        { 
         options.UseHttps("ProductionCertificate.pfx"); 
        } 
       }) 
      .UseIISIntegration() 
      .UseStartup<Startup>() 
      .Build() 
      .Run(); 
    } 
} 

Update

Я поднял следующую GitHub issue.

+0

Я думаю, вы можете сделать это только в режиме настройки – Alexan

ответ

2

Оказывается, вы можете использовать ConfigureServices разжиться IHostingEnvironment вот так:

public sealed class Program 
{ 
    public static void Main(string[] args) 
    { 
     IHostingEnvironment hostingEnvironment = null; 
     new WebHostBuilder() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .ConfigureServices(
       services => 
       { 
        hostingEnvironment = services 
         .Where(x => x.ServiceType == typeof(IHostingEnvironment)) 
         .Select(x => (IHostingEnvironment)x.ImplementationInstance) 
         .First(); 
       }) 
      .UseKestrel(
       options => 
       { 
        if (hostingEnvironment.IsDevelopment()) 
        { 
         // Use a self-signed certificate to enable 'dotnet run' to work in development. 
         options.UseHttps("DevelopmentCertificate.pfx", "password"); 
        } 
       }) 
      .UseIISIntegration() 
      .UseStartup<Startup>() 
      .Build() 
      .Run(); 
    } 
} 
0

Я попробовал это, и это было похоже на работу, но вы можете wan't перепроверить ...

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var config = new ConfigurationBuilder() 
         .AddEnvironmentVariables() 
         .Build(); 

     var pfx = config["ASPNETCORE_ENVIRONMENT"].Equals("Development", StringComparison.CurrentCultureIgnoreCase) 
      ? "DevelopmentCertificate.pfx" 
      : "ProductionCertificate.pfx"; 

     var host = new WebHostBuilder() 
      .UseKestrel(options => 
      { 
       options.UseHttps(pfx); 
      }) 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseIISIntegration() 
      .UseStartup<Startup>() 
      .Build(); 

     host.Run(); 
    } 
}