Identity – Authorize Custom Authorization Policy Providers
前言
上一篇讲完了基本使用 Policy-Based. 这一篇另外说一说怎么动态调用它.
参考: Custom Authorization Policy Providers using IAuthorizationPolicyProvider in ASP.NET Core
需求和实现思路
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AtLeast21", policy =>
policy.Requirements.Add(new MinimumAgeRequirement(21)));
});
比如说 21 这个变量我想通过 SQL 输出可以吗?
Policy 和 Requirement 的关系可以都保存在 SQL 吗? 然后要改就改.
依据上面的代码来分析出实现手法的话, 大概可以这样做, 在 builder.Services 的期间, 发请求到 SQL,
然后通过反射调用执行完上面的所有代码. 这样应该就 ok 了. 但即便是这样, 由于是在 StartUp setup 1 time, 除非重启, 不然这些配置是能即刻被修改的.
那 ASP.NET Core 是否提供了更便利的接口呢?
有的, 那就是 Authorization Policy Providers
我们实现一个 Policy Provider, 它的职责是, 当有人要求一个 Policy 时 (by name), Provider 就提供一个 Policy 实例.
这个 Policy 实例, 要通过 SQL 还是任何复杂逻辑都可以. 一个工厂模式就对了.
具体实现
Create PolicyProvider
public class MyPolicyProvider : DefaultAuthorizationPolicyProvider
{
public MyPolicyProvider(IOptions<AuthorizationOptions> options) : base(options)
{
} public override Task<AuthorizationPolicy?> GetPolicyAsync(string policyName)
{
return base.GetPolicyAsync(policyName);
}
}
底层接口是

GetPolicyAsync 就是依据 PolicyName 获取 Policy
GetDefaultPolicyAsync 则是 [Authorize] 这种没有声明 Policy 的
GetFallbackPolicyAsync 和 GetDefaultPolicyAsync 一样, 都是没有声明 Policy 的 autho, 只是它用于 Authorization Middleware
一般上都是继承 DefaultAuthorizationPolicyProvider 然后 override GetPolicyAsync 就够用了
Register Provider
必须是 Singleton 哦
builder.Services.AddSingleton<IAuthorizationPolicyProvider, MyPolicyProvider>();
由于是 Singleton 可能会无法 inject scope service, 解决方案是 Implementing a custom IAuthorizationPolicyProvider with database access
然后, 每一次只要有需要 Policy, PolicyProvider.GetPolicyAsync 就会被调用.

常见用法

自定义一个 AuthorizeAttribute, 然后动态声明 PolicyName

在通过 custom policy provider, 把这个名字拆开取出 age, pass to AgeRequirement. 这样就形成了一个 dynamic age requirement 验证.
Identity – Authorize Custom Authorization Policy Providers的更多相关文章
- Custom Data Service Providers
Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...
- ASP.NET Core3.1使用Identity Server4建立Authorization Server-2
前言 建立Web Api项目 在同一个解决方案下建立一个Web Api项目IdentityServer4.WebApi,然后修改Web Api的launchSettings.json.参考第一节,当然 ...
- ASP.NET Core3.1使用Identity Server4建立Authorization Server
前言 网上关于Identity Server4的资料有挺多的,之前是一直看杨旭老师的,最近项目中有使用到,在使用.NET Core3.1的时候有一些不同.所以在此记录一下. 预备知识: https:/ ...
- [转]How to: Create a Custom Principal Identity
本文转自:https://msdn.microsoft.com/en-us/library/aa702720(v=vs.110).aspx The PrincipalPermissionAttribu ...
- Filters in ASP.NET Core
Filters in ASP.NET Core allow code to be run before or after specific stages in the request processi ...
- Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]
Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...
- Enabling Cross-Origin Requests in ASP.NET Web API 2
Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...
- Filters in ASP.NET Core (转自MSDN)
Filters in ASP.NET Core MVC allow you to run code before or after specific stages in the request pro ...
- 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门
注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- ASP.NET Identity 一 (转载)
来源:http://www.cnblogs.com/r01cn/p/5194257.html 注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的A ...
随机推荐
- salesforce零基础学习(一百四十)Record Type在实施过程中的考虑
本篇参考: salesforce 零基础学习(二十九)Record Types简单介绍 https://help.salesforce.com/s/articleView?id=sf.customiz ...
- UDP协议测试
UDP协议测试 我们一般想到测试连通性时第一考虑到的就是使用ping命令. 但是我们知道ping命令使用的是icmp协议,属于tcp/ip协议中的一个子协议,所以我们可以用ping或tcping命令来 ...
- Java--普通方法重载
[转载自本科老师上课课件] 调用一个重载过的方法时,Java编译程序是如何确定究竟应该调用哪一个方法?以下代码定义了三个重载方法: public void f(char ch){ System.out ...
- BCLinux 8.2安装配置图解教程--龙蜥社区国产移动云系统
社区镜像下载地址:https://openanolis.cn/download 安装参考地址:https://www.osyunwei.com/archives/13017.html 1安装系统 界面 ...
- CentOS 7 下将 jar 包注册为服务
前提条件 因为 jar 包启动需要用到 jdk,所以服务器上必须要安装jdk或者jre,这方面的教程网上有非常多,可以去百度一下 创建文件 创建website.service文件, 内容如下: [ro ...
- 【Java】API SecureRandom 安全随机
之前学习的Random工具类只是一个伪随机数类... @Test public void secureRandom() throws Exception { // 个不可预测的安全的随机数 // 无法 ...
- 【Vue】14 UI库
PC端: 第一梯队:基于JQuery实现的Dom操作,和一些简单CSS样式组成 Layui Bootstrap EasyUI 第二梯队:基于Vue2.0开发的UI库,组件化开发 ElementUI A ...
- ComfyUI插件:ComfyUI layer style 节点(三)
前言: 学习ComfyUI是一场持久战,而ComfyUI layer style 是一组专为图片设计制作且集成了Photoshop功能的强大节点.该节点几乎将PhotoShop的全部功能迁移到Comf ...
- Longley数据集——强共线性的宏观经济数据,包含GNP deflator(GNP平减指数)、GNP(国民生产总值)、Unemployed(失业率)、ArmedForces(武装力量)、Population(人口)、year(年份),Emlpoyed(就业率)。LongLey数据集因存在严重的多重共线性问题,在早期经常用来检验各种算法或计算机的计算精度
Longley数据集来自J.W.Longley(1967)发表在JASA上的一篇论文,是强共线性的宏观经济数据,包含GNP deflator(GNP平减指数).GNP(国民生产总值).Unemploy ...
- 字符串系列目录&&说明
字符串准备写成一个系列. 目录 \(\text{KMP笔记}\) \(\text{Manacher笔记}\) [] [] [] 格式与说明 下面的说明和格式将被应用于整个系列. 说明 所有字符串的下标 ...