weixin_39646412
weixin_39646412
2021-01-07 01:49

Determining Hosting Environment While Configuring Kestrel and UseHttps

In the ASP.NET Core Main method below, how can I determine the hosting environment, so I can switch between different certificate files for HTTPS?


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();
    }
}
</startup>

该提问来源于开源项目:aspnet/KestrelHttpServer

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

20条回答

  • weixin_39618597 weixin_39618597 4月前

    UseIISIntegration will set PreferHostingUrls after we address https://github.com/aspnet/IISIntegration/issues/352

    点赞 评论 复制链接分享
  • weixin_39549852 weixin_39549852 4月前

    ahh thank you didn't know there was PreferHostingUrls thing any documentation on that?

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    it's new for 2.0: https://github.com/aspnet/Hosting/commit/4cdc970055ef2ad77887109c4acde89278e0ce18

    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    This covers https://github.com/aspnet/KestrelHttpServer/issues/1713 too.

    点赞 评论 复制链接分享
  • weixin_39956558 weixin_39956558 4月前

    Is is possible to set UseHttps from Startup class?

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    Yes:

    
    services.Configure<kestrelserveroptions>(options =>
     {
       options.UseHttps(...);
     });
    </kestrelserveroptions>
    点赞 评论 复制链接分享
  • weixin_39983554 weixin_39983554 4月前

    I was able to find a slightly shorter way before this gets implemented properly:

    
    public static IHostingEnvironment hostingEnvironment;
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<startup>()
            .ConfigureAppConfiguration((hostingContext, config) => {
                hostingEnvironment = hostingContext.HostingEnvironment;
            })
                .UseKestrel(options => {
                    if (hostingEnvironment.IsDevelopment()) {
                        options.Listen(IPAddress.Loopback, 443, listenOptions => {
                            listenOptions.UseHttps("certificate.pfx", "password");
                        });
                    }
                })
            .Build();
    </startup>
    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    Another way to do this:

    
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<startup>()
                .UseKestrel()
                .ConfigureServices((context, services) =>
                {
                    services.Configure<kestrelserveroptions>(options =>
                    {
                        if (context.HostingEnvironment.IsDevelopment())
                        {
                            options.Listen(IPAddress.Loopback, 443, listenOptions =>
                            {
                                listenOptions.UseHttps("certificate.pfx", "password");
                            });
                        }
                    });
                })
            .Build();
    </kestrelserveroptions></startup>
    点赞 评论 复制链接分享
  • weixin_39560207 weixin_39560207 4月前

    It was just answered on another thread that I just saw: https://github.com/aspnet/Home/issues/1915#issuecomment-276133606

    ... and if you don't use command-line config, env var config, a config file, etc., then you can read the ASPNETCORE_ENVIRONMENT env var directly after you manually set it somehow.

    点赞 评论 复制链接分享
  • weixin_39646412 weixin_39646412 4月前

    It turns out you can use ConfigureServices to get hold of IHostingEnvironment like so:

    
    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();
        }
    }
    </startup>

    It really feels like this should be easier.

    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    Assigning to for investigation

    点赞 评论 复制链接分享
  • weixin_39549852 weixin_39549852 4月前

    How can you handle the .UseUrls() to wrap an if statment around it?

    
    // Only add if development??
    
    .UseUrls("https://localhost:5001")
    
    点赞 评论 复制链接分享
  • weixin_39646412 weixin_39646412 4月前

    Couldn't you do something like:

    .UseUrls(hostingEnvironment.IsDevelopment() ? "http://*:8080" : "https://*:8080")

    You can get hostingEnvironment like I showed but this should really be easier.

    点赞 评论 复制链接分享
  • weixin_39549852 weixin_39549852 4月前

    that's pretty cool, but what happens if you publish to an IIS environment?

    点赞 评论 复制链接分享
  • weixin_39646412 weixin_39646412 4月前

    Well, that would be a Production environment, so in my example above it would use the https URL.

    点赞 评论 复制链接分享
  • weixin_39549852 weixin_39549852 4月前

    thanks for that, I wasn't sure if IIS would still be able to use the .UseUrls() for a live site like foo.com

    .UseUrls(hostingEnvironment.IsDevelopment() ? "http://*:8080" : "https://*:443")

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    IIS overwrites UseUrls.

    点赞 评论 复制链接分享
  • weixin_39549852 weixin_39549852 4月前

    ahh thank you I didn't know that. What about options.UseHttps() does that get overridden?

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    It will (though that may be broken right now in dev). Do you have UseIISIntegration setting the new PreferHostingUrls?

    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    has some ideas for another (nicer) workaround.

    点赞 评论 复制链接分享

相关推荐