Search code examples
c#authenticationasp.net-core-2.0startupcookie-authentication

.net core 2.0 cookie authentication getting stuck in infinite redirect loop when trying to access over https


I have just moved my code to our QA environment which uses https and what was working in Dev is not working in QA because the browser gets stuck in an infinite redirect loop. Our load balancer forces https so when the login redirect happens from code, which for some reason it's trying to redirect to http instead of https, the load balancer is stopping it and adding https again which causes the infinite loop. The question I have is why is this code not just redirecting to https, the path is relative in the ConfigureServices() method. I've looked at it in fiddler, and it is indeed adding the FQDN for the redirect with http instead of https.

Is there some property I need to add to options here to allow https redirects?

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = "/Account/LogIn";
                options.LogoutPath = "/Account/LogOff";
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseAuthentication();
    }

thanks.


Solution

  • We just use:

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {           
            ... //settings and logging initialization
            app.Use((context, next) =>
            {
                context.Request.Scheme = "https";
                return next();
            });
            ... //all the rest middleware calls
        }
    

    and it helps in most situations under OWIN and .Net Core up to 2.0