如何在 asp.net MVC 3 中实现/创建基于角色的自定义用户身份验证
如何在 asp.net MVC 3 中实现基于角色的自定义用户身份验证。考虑我有两个表 UserInfo(UserId, UserName, Password,RoleId)
和 Role(RoleId, RoleName).
我想从数据库(UserInfo
表)验证用户,并且还想从该表中检索角色。并想使用像 [Authorize(Roles="Admin")]
需要您的帮助和想法......
How can I implement role based custom user authentication in asp.net MVC 3. Consider I have two table UserInfo(UserId, UserName, Password,RoleId)
and Role(RoleId, RoleName)
.
I want to validate user from Database(UserInfo
Table) and also want to retrieve roles from that table. And want to use like[Authorize(Roles="Admin")]
Need your help and idea....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义授权属性并将角色存储在身份验证 cookie 的用户数据部分中。因此,例如在您的 LogOn 方法中,一旦您验证了凭据,您就可以从数据库中检索给定用户的角色并将它们存储到用户数据中:
然后您可以编写一个自定义授权属性,该属性将用于读取身份验证 cookie并提取角色信息:
现在剩下的就是用这个新属性装饰您的控制器/操作:
更新:
根据评论部分的要求,这里是您如何覆盖自定义授权中的
HandleUnauthorizedRequest
方法属性,以便如果用户无权访问给定操作,他将被重定向到某个错误视图而不是登录页面:You could use a custom authorize attribute and store the roles in the user data part of the authentication cookie. So for example inside your LogOn method once you have verified the credentials you could retrieve the roles for the given user from your database and store them into the user data:
Then you could write a custom authorize attribute which will be used to read the authentication cookie and extract the roles information:
Now all that's left is to decorate your controllers/actions with this new attribute:
UPDATE:
As requested in the comments section here's how you could override the
HandleUnauthorizedRequest
method in the custom authorize attribute so that if the user is not authorized to access a given action he is redirected to some error view instead of the login page: