如果用户已登录但没有相应的角色,我需要重定向到登录页面 . 这可以通过[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 回答
通过此代码解决: