Search code examples
asp.net-mvcasp.net-coreasp.net-identityasp.net-authorizationasp.net-authentication

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


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!


Solution

  • 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