ASP.NET MVC 中基于授权选择性渲染视图的策略?
我可以使用授权属性轻松地对控制器操作进行授权。 但是如何像使用授权属性一样轻松地呈现视图呢?
例如,如果我有一个菜单,并且用户未获得某些菜单的授权,我将如何隐藏这些菜单项?
假设我有一个页面,它呈现一个带有添加/编辑/删除链接的表格。我有与这些链接相对应的控制器操作,因此未经授权的用户无法执行这些操作。但我想更进一步,将它们完全隐藏起来。
我现在所拥有的是一团糟,它检查视图中的用户权限 - 一大堆 <% if (user.hasPermission(..) { %> ... <% } %>。有
什么策略可以缓解这种混乱吗?我当然不能是第一个陷入这种困境的人。
I can do authorization easily on the controller actions using the Authorization attribute.
But how do I render views as easily as using Authorization attribute?
For example, if I have a menu and the user is unauthorized for certain menus, how would I hide those menu items?
Let's say I have a page that renders a table with add/edit/delete links. I have controller actions that correspond to those links and so the unauthorized user cannot perform those actions. But I'd like to go further by hiding them altogether.
What I have now is a spaghetti of a mess that checks for user permissions in the view - whole bunch of <% if (user.hasPermission(..) { %> ... <% } %>. It's unruly.
What strategies are there to mitigate this mess? Certainly I can't be the first one to have run into this dilemma.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为“安全修剪”ActionLink 帮助程序可以很好地解决您的问题:
“安全感知”操作链接?
I think a "security trimming" ActionLink helper would solve your problem nicely:
"Security aware" action link?