为asp.net-core 2中的每个角色实现不同的登录页面 [英] Implement Different Login Page for each role in asp.net-core 2
问题描述
我想根据其在asp net core中的角色为每个用户实现不同的登录页面.我可以设置登录路径,但是它对于任何角色都是静态的.
I want to implement different login page for each user based in its role in asp net core . I can set login path but its static for any roles.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "Account/Login/";
options.AccessDeniedPath = "Account/Forbidden/";
});
因此,当我调用authorize(role ="Admin")重定向到管理员登录页面的操作时.然后当授权(role ="User")的呼叫操作重定向到用户登录页面
so when i call action that authorize(role="Admin") redirect to admin login page. and when call action that authorize(role="User") redirect to User login page
推荐答案
我像这样在启动ConfigureServices时添加了两种不同的身份验证方案
I add two different Authentication scheme in start up ConfigureServices like this
services.AddAuthentication(options =>
{
options.DefaultScheme = "UserAuth";
})
.AddCookie("UserAuth", options =>
{
options.LoginPath = "/Account/Login/";
options.AccessDeniedPath = "/Account/AccessDenied/";
})
.AddCookie("AdminAuth", options =>
{
options.LoginPath = "/Admin/Account/Login/";
options.AccessDeniedPath = "/Admin/Account/AccessDenied/";
});
使用管理员角色控制器进行授权时,我选择管理方案
When authorize with admin role controller i choose admin scheme
[Authorize(Roles = "Administrator", AuthenticationSchemes = "AdminAuth")]
使用用户角色控制器授权时,我选择用户方案
When authorize with user role controller i choose user scheme
[Authorize(Roles = "User", AuthenticationSchemes = "UserAuth")]
您可以查看此链接 查看全文