基于客户用户属性的用户视图限制
我正在使用 ASP.NET MVC 3。
我想创建一个操作过滤器来确定用户是否可以访问视图。我有一个 User 类,具有 IsAdministrator、IsTrusteeUser 和 IsAuditUser 等属性。如果某些用户不属于其中某些角色,我将如何创建操作过滤器来阻止某些用户?
另外,我如何在视图中使用它来隐藏/显示某些控件?我希望有一些代码:)
I'm using ASP.NET MVC 3.
I would like to create an action filter to determine if a user can access a view. I have a User class with properties like IsAdministrator, IsTrusteeUser and IsAuditUser. How would I create an action filter to block certain users if the don't belong in some of these roles?
And aslo how would I use this in my views to hide/display certain controls? I would appreciated some code :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要重新发明轮子?
将
[Authorize]
操作过滤器放在操作/控制器上,指定所需的角色:要么这样,要么您可以通过实现
IAuthorizationFilter
来实现您自己的自定义授权过滤器。Why re-invent the wheel?
Put the
[Authorize]
action filter on the action/controller, specifying the role required:Either that, or you could implement your own custom authorization filter by implementing
IAuthorizationFilter
.您可以实现 IActionFilter 接口来编写这样的属性扩展以进行用户访问权限检查,有关编码的一些信息可以在
这里
要隐藏/显示 UI 上的某些控件,这不是 ActionFilters 的工作,而是您应该为每个用户单独的视图并相应地重定向他或执行一些操作
要实现这一目标。
You can implement IActionFilter interface for writing such an attribute extension for Users access permissions checking, a little about coding you can find on
here
To hide/display certain controls on UI, it is not the work of ActionFilters, rather you should either make separate views for each user and redirect him accordingly or do some
to acheive this.