Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddRedirectToHttps() doesn't redirect to Https

The Enforcing SSL in a ASP.NET Core App guide on MSDN, tells me to add the following code to my Configure method in the Startup class, in order to redirect all http request to https:

var options = new RewriteOptions()
       .AddRedirectToHttps();

    app.UseRewriter(options);

Having added the code in the correct place, and testing a http request in debug mode, I get a connection reset error in chrome:

This site can’t be reached

The connection was reset.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_RESET

I'm trying to access the same URL (including port.. which I think is where I might be going wrong?) that I would if I was using https... I.E, I'm typing http://localhost:44376 instead of https://localhost:44376 into my address bar.

A slimmed down version of my Configuration method looks like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    var options = new RewriteOptions()
        .AddRedirectToHttps();

    app.UseRewriter(options);

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseIdentity();

    app.UseFacebookAuthentication(new FacebookOptions()
    {
        AppId = Configuration["Authentication_FacebookAppId"],
        AppSecret = Configuration["Authentication_FacebookAppSecret"]
    });

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
} 
like image 290
KidCode Avatar asked Jun 14 '17 19:06

KidCode


1 Answers

As this github post confirms, I was on the right lines thinking it was probably the port that was causing the issue. (Essentially, you can't listen for http and https requests on the same port.)

The fix for me was actually three-fold:

Firstly, you need to be careful how your running your app in development. When running visual studio on windows, the default is to startup using IIS / IIS Express. This causes a problem because it uses the application url defined in the project settings, rather than the urls we try to pass to kestrel via the startup class. (It's the applicationUrl defined in the iisSettings section of launchSettings.json)

If you expand the drop down on the start button inside Visual Studio, you should see an option with your project name, this starts your application using Kestrel via the dotnet CLI.

Secondly, you need to define two url's for kestrel to listen on, one for http and the other for https. This is done by simply passing in two url's, with different ports to the UseUrls() method in main():

var host = new WebHostBuilder()
    .UseKestrel(options => {
        options.UseHttps(certFile, certPass);
    })
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseUrls("https://*:44388", "http://*:8080")
    .UseStartup<Startup>()
    .UseApplicationInsights()
    .Build();

Finally, if your not using the default https port (443), you'll need to specify which port you want kestrel to redirect http request too. To do this, simply overload the AddRedirectToHttps() method, by passing in a status code and the port your wanting to redirect too. I've used the status code 301 to permanently redirect to https.

var options = new RewriteOptions()
.AddRedirectToHttps(301, 44388);
like image 166
KidCode Avatar answered Sep 23 '22 12:09

KidCode