MVC2 从 SQL 授权角色
我的 SQL2008 数据库中有一张用于用户的表,一张用于角色的表,然后是 UserRoles 桥接表。我现在必须 RoleProvider 才能工作,并用 [Authorize(Roles = "Administrator,Developer")] 装饰了我的一些操作,
我实际上在每个用户的网站上构建了导航,因此 RoleProvider 只是为了防止较低级别的用户从他的管理员伙伴那里获取 URL 并访问他不应该访问的页面。
我们在每个用户基础上构建站点导航,并在用户、其角色和允许其所在角色查看的页面之间建立映射。我只是想知道是否有任何方法可以更改 [Authorize(Roles = "")] 以从我的数据库动态获取有权执行该操作的角色列表?这样我就不必去装饰我拥有的所有动作,它只会像魔术一样从数据库中拉出。
一个简单的例子将不胜感激,谢谢。 杰克
I have a table in my SQL2008 DB for Users and one for Roles and then the UserRoles bridging table. I am at the point where I have to RoleProvider to work and have decorated some of my Actions with [Authorize(Roles = "Administrator,Developer")]
I actually build the navigation on my site per user so the RoleProvider is just to prevent a lower level user from getting the URL from his Admin buddy and going to a page that he is not supposed to.
We build the site navigation on a per user base and have a mapping between the user, his role and the pages that the role he is in is allowed to see. I just want to know if there is any way to change the [Authorize(Roles = "")] to get the list of roles with permission to that action dynamically from my database? That way I do not have to go decorate all actions that I have, it will just be pulled from the DB as if by magic.
A simple example will be appreciated, thank you.
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我基本上编写了自己的 CustomAuthorize 类,该类继承自 AuthorizeAttribute,并在 OnAuthorization 中查找访问权限。如果用户没有访问权限,我基本上会这样做:
filterContext.Result = new HttpUnauthorizedResult();
filterContext.Result = new RedirectResult("/accessDenied");
有效,我用以下方法装饰我的方法:[CustomAuthorize]
I basically wrote my own CustomAuthorize class that inherits from AuthorizeAttribute and in the OnAuthorization I did the look-up for access. If the user does not have access I basically do:
filterContext.Result = new HttpUnauthorizedResult();
filterContext.Result = new RedirectResult("/accessDenied");
Works, and I decorate my methods with: [CustomAuthorize]