ASP.NET MVC 6 Change Default Url Redirect When Not Logged In

774 Views Asked by At

I have a mvc project in developemnt, when a user isn't logged in and trys to go to a page that ISN'T the login or register (AllowAnonymous) they're automatcally redirected to ~/Account/Login?ReturnUrl=%2F"PageTryingToAccess".This is done via a authorization needed statement on the Program.cs.

My login page is the ~/home/(index), the default page on the (portal) website. Is there a way of overiding the ~/Account/Login default? I can not find where this is declared in the solution!

1

There are 1 best solutions below

2
Qing Guo On BEST ANSWER

Is there a way of overiding the ~/Account/Login default?

In .net6, you can add below code in Program.cs:

builder.Services.ConfigureApplicationCookie(opts =>
{
    opts.LoginPath = "/Home/Index";
   
});

In asp.net core3, you can use below:

 services.ConfigureApplicationCookie(opts =>
            {
                opts.LoginPath = "/Home/Index";                    
            });

Result:

enter image description here