ASP.NET 安全角色和权限
我对 ASP.NET 安全模型很满意,可以根据用户所处的角色允许/拒绝 web.config 中的用户访问,例如,
<system.web>
<authorization>
<allow roles = "Admin" />
</authorization>
</system.web>
但是我想要做的是为管理员用户提供一组权限,这些权限可以然后进行检查,例如具有“可以打印文档”、“可以删除文档”等权限的管理员用户
这种事情是否可以开箱即用,或者我是否需要走自定义路线?
I'm comfortable with the ASP.NET security model whereby one can allow/deny access to users in the web.config based on what roles they are in e.g.
<system.web>
<authorization>
<allow roles = "Admin" />
</authorization>
</system.web>
However what I want to do is give the admin user a set of permissions which can then be checked e.g. an Admin user with permissions like "can print documents", "can delete document"
Is this sort of thing possible out of the box or would I need to go down a custom route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以按照这篇 MSDN 文章中的说明使用 Azman。
但 Azman 有很多地方我不喜欢,因此我推出了自己的角色作为 RoleProvider 的补充(用于管理权限到角色映射的附加表、API 和管理工具)。
我的自定义实现非常简单:
角色和权限之间的MN关系。
API“HasPermission”,用于测试给定主体是否具有给定权限。 这只是迭代所有角色并检查该角色是否具有给定的权限。 出于性能原因,映射权限角色使用 ASP.NET 缓存进行缓存。
You can use Azman as described in this MSDN article.
But there are a number of things I don't like about Azman, so I rolled my own as a complement to the RoleProvider (additional tables, APIs and admin tools that manage the mapping of permissions to roles).
My custom implementation is very simple:
M-N relationship between roles and permissions.
An API "HasPermission" that tests if a given principal has a given permission. This simply iterates through all roles and checks if the role has the given permission. The mapping permission-roles is cached using the ASP.NET cache for performance reasons.
它不是开箱即用的。 但如果您想要更细化,为什么不使用“CanPrint”、“CanDelete”等细化角色,而不是“Admin”等更广泛的角色呢?
如果他们想要一个容器类型场景,正如您在评论中指出的那样,您可以设置一个自定义 IPrincipal - 在身份验证之后,并且对于每个新请求,您都会查看用户的角色成员身份(“管理员”、“公共”等),然后覆盖您的 IPrincipal 上的 IsInRole。 您可以在此处找到示例
It's not there out of the box; but if you wanted to be more granular, why not have granular roles like "CanPrint", "CanDelete" rather than wider ones like "Admin"?
If they want a container type scenario as you indicate in your comments you could setup a custom IPrincipal - where, after authentication, and with each new request you look at the user's role membership ("Admin", "Public" etc.) and then override IsInRole on your IPrincipal. You can find an example here
我发现这篇文章举了一个很好的例子
i found this article that gives a nice example
您可以在 RoleProvider 中返回 PERMISSIONS 而不是 ROLES。
然后创建管理页面,向角色添加 {granted/denied} 权限,当然还有向角色添加用户权限。
You could return PERMISSIONS instead of the ROLES in your RoleProvider.
Then create your admin pages to add {granted/denied} permissions to roles and of course users to roles.
是的,这是可能的。 创建所需的角色,将用户添加到角色,然后只需在执行需要该角色的操作的代码中检查 User.IsInRole 即可。
查看 System.Web.Security 中的 Roles 和 MemberShip 类
Yes it's possible. Create the roles you want, add the users to the roles, and then just check User.IsInRole in your code where you perform the action that requires that role.
Take a look at the Roles and MemberShip classes in System.Web.Security