iauthorizedate接口代表了授权系统的源头:

public interface iauthorizedata
{
  string policy { get; set; }
  string roles { get; set; }
  string authenticationschemes { get; set; }
}

接口中定义的三个属性分别代表了三种授权类型:

1、基于角色的授权:

[authorize(roles = "admin")] // 多个role可以使用,分割
public class sampledatacontroller : controller
{
  ...
}

2、基于scheme的授权:

[authorize(authenticationschemes = "cookies")] // 多个scheme可以使用,分割
public class sampledatacontroller : controller
{
  ...
}

3、基于策略的授权:

[authorize(policy = "employeeonly")]
public class sampledatacontroller : controller
{
  
}

基于策略的授权是授权的核心,使用这种授权策略时,首先要定义策略:

public void configureservices(iservicecollection services)
{
  services.addmvc();

  services.addauthorization(options =>
  {
    options.addpolicy("employeeonly", policy => policy.requireclaim("employeenumber"));
  });
}

授权策略本质上就是对claims的一系列断言。

而基于角色和基于scheme的授权都是一种语法糖,最终会转换为策略授权。

以上就是关于asp.net core 授权的知识点内容