Custom absolute login URL in ASP.NET Core 2.0

Viewed 891

Is it possible to specify an absolute redirect login URL in ASP.NET Core 2? The standard method does not accept absolute URLs

services.ConfigureApplicationCookie(x => {
    x.LoginPath = new PathString("https://localhost:88/account/login");
});  

Produces a runtime error as the URL is expected to start with "/".

The initial problem is that my app service is served over HTTPS, which terminates at CloudFlare, so when user is not authorised, ASP.NET redirects the user to HTTP address, rather than to HTTPs.

1 Answers

You can override the application host name for each request in the Configure() method of Startup.cs

app.Use((ctx, next) =>
{
    ctx.Request.Host = new HostString("https://localhost:88");
    return next();
});

and then in your ConfigureServices() method:

services.ConfigureApplicationCookie(x => {
    x.LoginPath = new PathString("/account/login");
});

I had a similar problem with Application Gateway, which terminated the SSL connection to a custom domain then forwarded the request on to an Azure Web App at the standard http://*.azurewebsites.net domain.

See my original problem here.

Related