Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Authorize Redirection to wrong URL

I am trying to run a web application with the following route mapped:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                "default",
                "WoL/{controller=Account}/{action=Login}/{id?}");
        });

If the user is not authenticated and tries to access a action having the AuthorizeAttribute, the user should be redirected to the default login URL (as seen above). But the user gets redirected to "/Account/Login" instead of "/WoL/Account/Login". How can I redirect the user to "/WoL/Account/Login", if the user is not authenticated? I have configured the following Cookie Authentication:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            LoginPath = new PathString("/WoL/Account/Login"),
            AutomaticChallenge = true
        });
like image 290
Thomas Gassmann Avatar asked Dec 07 '16 12:12

Thomas Gassmann


2 Answers

The answer of @Dmitry is not working anymore in ASP.NET Core 3.1. Based on the documentation that you can find here, you have to add the following code to the ConfigureServices:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest)
   .AddRazorPagesOptions(options =>
   {
       options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
       options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

    services.ConfigureApplicationCookie(options =>
    {
       options.LoginPath = $"/Identity/Account/Login";
       options.LogoutPath = $"/Identity/Account/Logout";
       options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
     });
like image 129
bertilvandekerkhove Avatar answered Oct 20 '22 05:10

bertilvandekerkhove


This works for me (in Startup.ConfigureServices):

services.AddIdentity<User, UserRole>(options => 
{
    options.Cookies.ApplicationCookie.LoginPath = new PathString("/Admin/Account/Login");
});
like image 5
Dmitry Avatar answered Oct 20 '22 05:10

Dmitry