架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 13154|回复: 2

[资料] Role based Authorization 基于角色的授权

[复制链接]
发表于 2020-6-7 21:25:56 | 显示全部楼层 |阅读模式
133 of 153 people found this helpful
When an identity is created it may belong to one or more roles, for example Tracy may belong to the Administrator and User roles whilst Scott may only belong to the user role. How these roles are created and managed depends on the backing store of the authorization process. Roles are exposed to the developer through the IsInRole property on the ClaimsPrincipal class.

新建的身份可以属于一个或多个角色,例如,Tracy可属于Administrator和User角色,Whilst Scott可仅属于User角色。如何新建和管理这些角色依靠授权过程是如何存储的。通过ClaimsPrincipal类的IsInRole方法向开发者提供了角色的使用方法。

Adding role checks 添加角色验证

Role based authorization checks are declarative - the developer embeds them within their code, against a controller or an action within a controller, specifying roles which the current user must be a member of to access the requested resource.

基于角色的验证是基于声明的,开发者将其嵌入到代码中,对一个控制器或其中的方法指定角色,指定一个请求中的用户必须满足相应的成员要求。

For example the following code would limit access to any actions on the AdministrationController to users who are a member of the Administrator group.

例如下列代码将限制AdministrationController 中的任何一个方法,必须是Administrator 组的成员才可以使用。



You can specify multiple roles as a comma separated list;

你可将多个指定的角色到一个逗号分割的列表中:



This controller would be only accessible by users who are members of the HRManager role or the Finance role.

该控制器将仅能被HRManager 角色或 Finance 角色的成员访问。

If you apply multiple attributes then an accessing user must be a member of all the roles specified; the following sample requires that a user must be a member of both the PowerUser and ControlPanelUser role.

如果你使用了多个属性,则访问用户必须属于所有角色的成员;下面的例子需要一个用户必须同时是PowerUser和ControlPanelUser角色的成员。



You can further limit access by applying additional role authorization attributes at the action level;

你可在方法层级上使用附加的角色授权属性来应用更多的使用限制;




In the previous code snippet members of the Administrator role or the PowerUser role can access the controller and the SetTime action, but only members of the Administrator role can access the ShutDown action.

在前面的代码片段中,Administrator角色或者PowerUser角色的成员可使用该控制器和SetTime方法,但是仅有Administrator角色的成员可以使用ShutDown方法。

You can also lock down a controller but allow anonymous, unauthenticated access to individual actions.

你也可封锁一个控制器,但允许匿名用户非授权地使用单独的方法。



Policy based role checks 基于策略的角色检查

Role requirements can also be expressed using the new Policy syntax, where a developer registers a policy at startup as part of the Authorization service configuration. This normally takes part in ConfigureServices() in your Startup.cs file.

对角色的要求也可通过使用新的策略语法来实现,开发者在startup中将一个策略注册为授权服务配置的一个部分。这通常加入到Sartup.cs文件的ConfigureServices()中。



Policies are applied using the Policy property on the AuthorizeAttribute attribute;

通过在AuthorizeAttribute属性上使用Policy属性实现策略。



If you want to specify multiple allowed roles in a requirement then you can specify them as parameters to the RequireRole method;

如果你想在一个请求中指定多个角色,你可将其指定为RequireRole方法的多个参数:



his example authorizes users who belong to the Administrator, PowerUser or BackupAdministrator roles.

这个例子中的授权用户将属于Administrator,PowerUser或者 BackupAdministrator 角色。

转自:https://www.cnblogs.com/jqdy/p/5989257.html




上一篇:解决HttpContext.User.IsInRole() 永远返回false的问题
下一篇:TS Missing radix parameter (radix)
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2020-6-7 21:36:01 | 显示全部楼层


如果只想应用到最后一个特性,也就是:

可以在自定义过滤器上面加上:


AllowMultiple

这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2020-6-10 11:46:34 | 显示全部楼层
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-26 18:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表