How do I change the Identity authorization path in ASP.NET Core?

Viewed 295

I created an Account folder with an AccountController, Login and Register views, and links to those views on my layout page. That all work fine, and the path looks like this:

localhost/Account/Login

My problem started when I added [Authorize] to a controller. When I click on a link handled by that controller, instead of routing to my Login view, it routes to:

localhost/Identity/Account/Login

Is there somewhere in Startup.cs or some other file where I can change that default path so that it routes to my Login page instead?

2 Answers

You can configure the login path by configuring the cookie options:

in your Startup.cs

services.ConfigureApplicationCookie(options =>
            {
                options.AccessDeniedPath = "/access-denied";
                options.LoginPath = "/login";
                options.LogoutPath = "/logout";
            });

In your Startup.cs class

In this method

public void ConfigureServices(IServiceCollection services)

You need to add

services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/Account/Login";
            });
          
Related