MVC应用中的安全系统设计
可以使用哪种设计方法(已经存在的还是新的设计方法)来在 MVC Web 应用程序中实现安全系统?
可能存在一些模式、最佳实践,例如在一些流行语言中,例如Java、.Net 或其他语言? 在哪个级别上实现它更好:模型或控制器或它们之间的某个级别?
我面临着这种原始方法会导致根据实现在许多控制器或模型之间传播安全检查,并与级别代码混合。 但对于我来说,如何更好地设计安全性并不明显。
我有一个 MVC 应用程序。 我需要灵活的访问权限系统。 我有类别和类别中的实体的层次结构。 某些用户可以编辑/查看/添加/删除一组类别中的一个,另一个用户可以编辑/查看/添加/删除其他类别。 还要求根据用户角色,不应从数据库检索模型的某些字段(应返回 null) 管理员应该能够分配不同的访问权限。
Which design approach can be used, already exists or new ones to implement security system in MVC web-application?
Possibly there are patterns, best practices, e.g. in some popular languages, e.g. Java, .Net or whatsoever?
On which level is it better to implement it: model or controller or something between them?
I faced that primitive approach results in spreading security checks among many controllers or models depending on implementation, mixing with code of levels.
But it is not obvious for me in which way to design security in better way.
I have an MVC application.
I need flexible system of access rights.
I have and hierarchy of categories and entities in categories.
Some user can edit/view/add/remove one on set of categories, another users - other categories.
It is also required that depending on user role some fields of model should not be retrieved from DB (null should be returned)
Admins should be able to assign different access rights.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用.Net 内置的会员服务提供商。默认情况下,新的 MVC 3 Internet 应用程序 Visual Studio 项目模板将为您提供基本的登录代码。在 Visual Studio 中单击“管理”站点按钮将为您提供用于管理用户和角色的 Web 界面。创建您的用户/角色,然后在您的控制器类定义或方法定义上定义 [Authorize(Roles="Admin,Users")]。不要像在 Asp.Net Web 表单应用程序中那样定义 URL 访问,因为有多个 URL 可以映射到单个位置。请改用授权属性。
另请检查我的回复:
在互联网上打开 ASP.Net 应用程序之前,我需要具备哪些功能?
另外,请确保您使用 Html.AntoForgeryToken在您的视图和 [HttpPost] 控制器方法上的 [ValidateAntiForgeryToken] (即您回发到的任何方法)
You should be using .Net's built in member ship providers. By default a new MVC 3 Internet Application Visual Studio project template will give you basic login code. Clicking on the 'manage' your site button from within visual studio will give you the web interface to manage the users and roles. Create your users/roles, then define [Authorize(Roles="Admin,Users")] on your Controller class definition or on your method definitions. DO NOT define URL access as was done in Asp.Net Web Form applications as there are multiple URLs that could map to a single place. Use the Authorize attribute instead.
Also check my response here:
What features do I need to have before I open an ASP.Net app onto the internet?
In addition make sure you use Html.AntoForgeryToken in your views and [ValidateAntiForgeryToken] on your [HttpPost] controller methods (ie any methods you post back to)