为asp.net

编程入门 行业动态 更新时间:2024-10-24 04:50:21
本文介绍了为asp-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")]

您可以查看此链接如何在ASP.NET Core 2.0中设置多种身份验证方案?

更多推荐

为asp.net

本文发布于:2023-11-16 18:38:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!