IAuthorizeDate接口代表了授权系统的源头: public interface IAuthorizeData{ string Policy { get; set; } string Roles { get; set; } string AuthenticationSchemes { get; set; }} 接口中定义的三个属性分别代表了三种授权
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 授权的知识点内容,如果大家有任何疑问可以联系易盾网络小编。
