Search code examples
c#asp.net-mvcasp.net-coreasp.net-identity

How to fix a bug in default login route at identity 2


I create my first project using asp net core. I installed Identity 2 for authentication in my project. As you know in identity 2 use Razor pages and all of the identities pages move to an area called Identity.

I mean the route to access to the login page on MVC and entity framework was "/Account/Login" but in asp net core is /Identity/Account/Login.

I created a new controller called MenusController and I added [Authorize] attribute to the class. But when I am not signed in and I try to call the MenusController Identity 2 Automatically called the "/Account/Login?ReturnUrl=%2FAdmin%2FMenus" and as you know the identity moved to the area "Identity" my website shows 404 pages not found an error.

I searched the entire solution to find how can I change the "/Account/Login?ReturnUrl=%2FAdmin%2FMenus" to Identity/Account/Login but I cant find it.

Do you know how can I change the default route for login in identity 2?


Solution

  • Move services.ConfigureApplicationCookie after services.AddIdentity and most important remove AddDefaultUI in services. Reference here

    Here is the code to change the default Login page:

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