Redirect to login when unauthorized in ASP.NET Core

asp.net, asp.net-core, asp.net-core-mvc, asp.net-identity, c#

Solution

You can configure the path using `CookieAuthenticationOptions` class.

Something like this.

app.UseCookieAuthentication(new CookieAuthenticationOptions {
        LoginPath = new PathString("/Login/"),
        AuthenticationType = "My-Magical-Authentication",
        // etc...
        },
});

Here is the updated link for CookieAuthenticationHandler

Problem

In the previous ASP.NET MVC, there was an option to redirect to the login action, if the user was not authenticated. I need the same thing with ASP.NET Core, so I: - created a ASP.NET Core project from the Visual Studio template - added `[Authorize]` to some arbitrary action - opened the corresponding view in my browser I don't expect a redirect because I haven't configured it. BUT, it automatically redirects to the login action! Where/how is this option set?

Original source

Related problems