首页 文章

如果未经授权,需要重定向登录Ms.AspNetCore.Identity 2.0

提问于
浏览
1

如果用户已登录但没有相应的角色,我需要重定向到登录页面 . 这可以通过[AuthorizeAttribute]在.net core 1.1.2中轻松实现 .

[Authorize(Roles="Manager")]

如果我使用asp.net核心Identity 2.0.1引导.Net Core 2.0 Web应用程序,则重定向到/ Account / AccessDenied,而不是Login .

这是来自Startup.cs:

services.AddIdentity<ApplicationUser, IdentityRole>(
            options => {
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 4;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireLowercase = false;
            }
            )
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

app.UseAuthentication();

1 回答

  • 1

    通过此代码解决:

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

相关问题