简体   繁体   English

如果用户登陆受保护页面,那么如何根据用户角色进行重定向?

[英]If user lands on protected page then how to redirect based on user role?

I'm using Asp.net Identity. 我正在使用Asp.net身份。

Currently have 7 types or roles in the system. 目前在系统中有7种类型或角色。 A user can only have 1 role. 一个用户只能有1个角色。 How can I set the redirect URL based on the user role if that user lands on a protected page that his role doesn't have privilege to? 如果该用户登录到该角色没有权限的受保护页面,该如何根据该用户角色设置重定向URL?

For example a Student user of role Student goes to Teacher.aspx . 例如,角色为StudentStudent用户访问了Teacher.aspx He is not authorized so the default is that he is redirected to the login page. 他没有被授权,因此默认值为将他重定向到登录页面。 But I would much rather redirect him somewhere else depending on his role because he is already logged in. 但是我宁愿根据他的角色将他重定向到其他地方,因为他已经登录。

User the System.Web.Mvc.AuthorizeAttribute attribute. 使用System.Web.Mvc.AuthorizeAttribute属性。 You can use it like so: 您可以这样使用它:

[Authorize(Roles="AuthorizedRoleOne,AuthorizedRoleTwo")]
public ActionResult Index()

By default it will redirect unauthorized users to a login page. 默认情况下,它将未经授权的用户重定向到登录页面。 If that isn't what you want have a look at this question (you can inherit from the attribute and change the behavior). 如果这不是您想要的,请查看此问题 (您可以从属性继承并更改行为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM