ASP.NET core, change default redirect for unauthorized

前端 未结 7 806
抹茶落季
抹茶落季 2020-12-05 09:55

I am attempting to redirect to a different login url in ASP.NET MVC6

My account controller login method has a Route attribute to change the url.

相关标签:
7条回答
  • 2020-12-05 10:07

    Since asp.net core 2.0 if you use cookies without Identity:

    app.UseAuthentication();
    
    // If you don't want the cookie to be automatically authenticated and assigned HttpContext.User, 
    // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options => 
        {
            options.LoginPath = "/Account/LogIn";
            options.LogoutPath = "/Account/LogOff";
        });
    

    source

    0 讨论(0)
  • 2020-12-05 10:08

    I wouldn't recommend Serj Sagan solution in a real life example. This would work perfectly when developing but for a real application used by different types of user that might be misleading. Lets look at the below scenario

    1. I am authenticated used
    2. I know the url for a specific page
    3. I am not authorize to access that pages

    It means that I would be redirected to the login page as if I were not authenticated which is not the case. I would go more with mxmissile solution

    Personnally I am using the AddMvcCore but you need to add AddRazorViewEngine if you are using razor views and AddRazorPages if you are using razor pages

            services.AddMvcCore(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            })
            .AddRazorViewEngine()
            .AddAuthorization()
            .AddJsonFormatters();
    
    0 讨论(0)
  • 2020-12-05 10:12

    With asp.net core 2.0 out now, this has changed to:

    services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");
    

    More on migrating to 2.0 here. And even more information on migrating from 2.0 to 2.1.

    0 讨论(0)
  • 2020-12-05 10:13

    You'll need to configure this in startup.cs when adding the authentication service especially if you're using cookie authentication scheme.

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options => 
            {
                options.LoginPath = new PathString("/login");
            }); 
    

    This was how i solved the issue, you'll should try it out...It'll definitely work for you

    0 讨论(0)
  • 2020-12-05 10:20

    You may also want to try using StatusCodePages:

    app.UseStatusCodePages(async context => {
        var response = context.HttpContext.Response;
    
        if (response.StatusCode == (int)HttpStatusCode.Unauthorized || 
            response.StatusCode == (int)HttpStatusCode.Forbidden)
            response.Redirect("/Error/Unauthorized");
    });
    
    0 讨论(0)
  • 2020-12-05 10:26

    UPDATE: As of dot net core 2.1.x, Identity is scaffolded from the SDK. To co--sign @mxmissile answer, the path can be specified. To pull off a trick path, combine with advanced routing or redirects.Scaffold Identity

    0 讨论(0)
提交回复
热议问题