Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Determining Hosting Environment While Configuring Kestrel and UseHttps #1334

Closed
RehanSaeed opened this issue Jan 31, 2017 · 20 comments
Closed

Comments

@RehanSaeed
Copy link

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();
    }
}
@guardrex
Copy link

It was just answered on another thread that I just saw: dotnet/aspnetcore#1915 (comment)

... 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.

@RehanSaeed
Copy link
Author

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

It really feels like this should be easier.

@muratg muratg added this to the 2.0.0 milestone Feb 3, 2017
@muratg
Copy link
Contributor

muratg commented Feb 3, 2017

Assigning to @CesarBS for investigation

@dotnetshadow
Copy link

dotnetshadow commented Apr 6, 2017

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

// Only add if development??

.UseUrls("https://localhost:5001")

@RehanSaeed
Copy link
Author

RehanSaeed commented Apr 6, 2017

@dotnetshadow 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.

@dotnetshadow
Copy link

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

@RehanSaeed
Copy link
Author

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

@dotnetshadow
Copy link

@RehanSaeed 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")

@Tratcher
Copy link
Member

Tratcher commented Apr 6, 2017

IIS overwrites UseUrls.

@dotnetshadow
Copy link

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

@Tratcher
Copy link
Member

Tratcher commented Apr 6, 2017

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

@muratg muratg modified the milestones: 2.1.0, 2.0.0 Apr 7, 2017
@muratg muratg assigned davidfowl and unassigned cesarblum Apr 7, 2017
@muratg
Copy link
Contributor

muratg commented Apr 7, 2017

@davidfowl has some ideas for another (nicer) workaround.

@JunTaoLuo
Copy link
Contributor

@Tratcher UseIISIntegration will set PreferHostingUrls after we address aspnet/IISIntegration#352

@dotnetshadow
Copy link

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

@Tratcher
Copy link
Member

Tratcher commented Apr 8, 2017

@dotnetshadow it's new for 2.0:
aspnet/Hosting@4cdc970

@muratg
Copy link
Contributor

muratg commented Jun 23, 2017

This covers #1713 too.

@libsamek
Copy link

Is is possible to set UseHttps from Startup class?

@Tratcher
Copy link
Member

Yes:

services.Configure<KestrelServerOptions>(options =>
 {
   options.UseHttps(...);
 });

@muratg muratg assigned halter73 and unassigned davidfowl Aug 18, 2017
@muratg muratg removed this from the 2.1.0 milestone Nov 10, 2017
@muratg muratg added this to the 2.1.0-preview1 milestone Nov 10, 2017
@davidh12
Copy link

@RehanSaeed 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();

@Tratcher
Copy link
Member

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();

@muratg muratg removed this from the 2.1.0-preview1 milestone Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests