Role based Authorization 基于角色的授权

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 组的成员才可以使用。

[Authorize(Roles = "Administrator")]
public class AdministrationController : Controller
{
}

You can specify multiple roles as a comma separated list;

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

[Authorize(Roles = "HRManager,Finance")]
public class SalaryController : Controller
{
}

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角色的成员。

[Authorize(Roles = "PowerUser")]
[Authorize(Roles = "ControlPanelUser")]
public class ControlPanelController : Controller
{
}

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

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

[Authorize(Roles = "Administrator, PowerUser")]
public class ControlPanelController : Controller
{
public ActionResult SetTime()
{
} [Authorize(Roles = "Administrator")]
public ActionResult ShutDown()
{
}
}

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.

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

[Authorize]
public class ControlPanelController : Controller
{
public ActionResult SetTime()
{
} [AllowAnonymous]
public ActionResult Login()
{
}
}

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()中。

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole", policy => policy.RequireRole("Administrator"));
});
}

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

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

[Authorize(Policy = "RequireAdministratorRole")]
public IActionResult Shutdown()
{
return View();
}

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

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

options.AddPolicy("ElevatedRights", policy =>
policy.RequireRole("Administrator", "PowerUser", "BackupAdministrator"));

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

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

原文链接

Security » Authorization » 基于角色的授权的更多相关文章

  1. Security » Authorization » 基于声明的授权

    Claims-Based Authorization¶ 基于声明的授权 142 of 162 people found this helpful When an identity is created ...

  2. Security » Authorization » 基于视图的授权

    View Based Authorization¶ 基于视图的授权 44 of 46 people found this helpful Often a developer will want to ...

  3. Security » Authorization » 基于资源的授权

    Resource Based Authorization¶ 基于资源的授权 68 of 73 people found this helpful Often authorization depends ...

  4. ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇

    在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...

  5. ASP.NET Identity 身份验证和基于角色的授权

    ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...

  6. ASP.NET Core 2.1中基于角色的授权

    ASP.NET Core 2.1中基于角色的授权 授权是来描述用户能够做什么的过程.例如,只允许管理员用户可以在电脑上进行软件的安装以及卸载.而非管理员用户只能使用软件而不能进行软件的安装以及卸载.它 ...

  7. Security » Authorization » 基于自定义策略的授权

    Custom Policy-Based Authorization¶ 基于自定义策略的授权 98 of 108 people found this helpful Underneath the cov ...

  8. Asp.net中基于Forms验证的角色验证授权

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活. Forms 验证方式对基于用户的验证授 ...

  9. 在中间层 .NET 应用程序中通过授权管理器使用基于角色的安全

    基于角色的安全是从 Windows NT 的第一个版本开始在 Windows 平台上发展而来的.使用角色,操作系统可以通过检查称为 BUILTIN\Administrators 的组的安全上下文做出一 ...

随机推荐

  1. SQL Server 导入 MDF LDF文件

    EXEC sp_attach_db @dbname = 'OA', @filename1 = 'C:\OA.mdf', @filename2 = 'C:\OA_log.ldf'

  2. Struts(八):动态方法调用

    动态方法调用:通过url动态调用action中的方法. 默认情况下,Struts的动态方法调用处于禁用状态. 测试定义一个action类: package com.dx.actions; public ...

  3. Apache和IIS服务器共存问题--来自网上内容

    1.apache 主要支持PHP   IIS 主要支持asp  静态的网页他们都支持入htm,端口冲突的话你可以更改其中一个服务器的端口来实现端口的避让,IIS在右击“我的电脑”“管理”“服务和应用程 ...

  4. 20145320 《Java程序设计》第10周学习总结

    20145320 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 计算机网络概述 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 按照计算机网络的定义 ...

  5. BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  6. ligerui_ligerTree_005_动态增加“树”节点

    动态添加ligerTree节点:效果图: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 <%@ page language= ...

  7. 夺命雷公狗-----React---9--map数据的遍历

    比如我们要实现的是这种效果: 用这种方法来写,她只能写死在哪,没啥意思,所以我们定义一个数据,然后来测试下map方法对她遍历出来的数据 <!DOCTYPE html> <html l ...

  8. 设置IE默认文本模式的方法

    设置IE默认文本模式的方法 <meta http-equiv="X-UA-Compatible" content="IE=8" /> IE=5.6. ...

  9. ThinkPHP讲解(十一)——验证码和文件上传

    一.验证码 1.页面前端显示 (验证码是图片标签,来源是控制器里的yzm()操作方法) <h1>登录</h1> <form action="__ACTION__ ...

  10. Linq join

    MXS&Vincene  ─╄OvЁ  &0000022─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...