asp.net core 授权详解

所属分类: 网络编程 / ASP.NET 阅读数: 575
收藏 0 赞 0 分享

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 授权的知识点内容,如果大家有任何疑问可以联系脚本之家小编。

更多精彩内容其他人还在看

Asp.net中的mail的发送

Asp.net中的mail的发送
收藏 0 赞 0 分享

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享
查看更多