Search code examples
asp.net-core-mvcasp.net-identityasp.net-authorization

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


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?


Solution

  • In your Startup.cs class

    In this method

    public void ConfigureServices(IServiceCollection services)
    

    You need to add

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