为所有 Gets on web Api Net Core 授权

Authorize for all Gets on webApi NetCore

我不知道这是否可能,但是,我可以为我的 webApi 中的所有 GET 操作制定类似默认策略的东西吗,所以它们需要一个特定的角色,一个用于“读”、“写”和“删除”。

不在组件的每个操作中放置 [Authorize()],更像是 StartUp 的 AddAuthorization 中的策略。

谢谢。

I don't know if it's possible, but, could I make something like a default policy for all my GET actions in my webApi, so they require a especific role, one for "read", "write" and "delete".

是的,您可以将基于策略的授权与基于角色的授权一起使用。

Not putting a [Authorize()] in each action of a component, more like a policy in the AddAuthorization of the StartUp.

Startup.ConfigureServices方法中的services.AddAuthorization()方法用于定义策略,之后策略应用到控制器 通过使用 [Authorize] 属性和策略名称。

这里有一个在Asp.net Core MVC中使用基于角色授权的基于策略授权的示例,您可以参考它。

  1. 创建一个 ASP.NET MVC 核心应用程序(使用个人用户帐户身份验证)。

  2. 使用迁移生成身份用户表。

  3. 单击项目并select添加,然后添加脚手架项和select身份,select覆盖所有文件,并添加身份模板页面.

  4. 为角色创建视图和控制器:

     public class RoleController : Controller
     {
         RoleManager<IdentityRole> roleManager;
    
         public RoleController(RoleManager<IdentityRole> roleManager)
         {
             this.roleManager = roleManager;
         } 
         public IActionResult Index()
         {
             var roles = roleManager.Roles.ToList();
             return View(roles);
         } 
         public IActionResult Create()
         {
             return View(new IdentityRole());
         }
    
         [HttpPost]
         public async Task<IActionResult> Create(IdentityRole role)
         {
             await roleManager.CreateAsync(role);
             return RedirectToAction("Index");
         }
     }
    

    使用RoleManager管理角色,添加相关视图。

    修改Startup.cs个文件,如下图:

         services.AddDbContext<ApplicationDbContext>(options =>
             options.UseSqlServer(
                 Configuration.GetConnectionString("DefaultConnection")));
         services.AddDatabaseDeveloperPageExceptionFilter();
    
         services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
              .AddDefaultUI()
              .AddEntityFrameworkStores<ApplicationDbContext>()
              .AddDefaultTokenProviders(); 
    
         services.AddControllersWithViews();
         services.AddRazorPages();
    

    在注册页面,您可以添加一个属性来使用select角色,并使用UserManager.AddToRoleAsync()方法为用户添加角色。

    之后,我们可以创建作用于 RoleController 的角色,如下所示:

  5. 配置身份验证:在 ConfigureServices 中定义以下策略:

         services.AddAuthorization(options => {
             options.AddPolicy("readpolicy",
                 builder => builder.RequireRole("Admin", "Manager", "User"));
             options.AddPolicy("writepolicy",
                 builder => builder.RequireRole("Admin", "Manager"));
         });
    

    然后,您可以将这些策略应用于 Controller 或 Action 方法。

         [Authorize(Policy = "readpolicy")]
         public IActionResult Index()
         {
             var roles = roleManager.Roles.ToList();
             return View(roles);
         }
         //[Authorize(Policy = "writepolicy")]
         [Authorize(Roles ="User")]
         public IActionResult Create()
         {
             return View(new IdentityRole());
         }
    

    之后,如果当前用户角色无权访问相关操作方法,则会显示“拒绝访问”:

参考:

Adding Role Authorization to a ASP.NET MVC Core Application

Policy-Based And Role-Based Authorization In ASP.NET Core 3.0 Using Custom Handler

Policy-based authorization in ASP.NET Core