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 ...
随机推荐
- [oeasy]python0136_接收输入_input函数_字符串_str
输入变量 回忆上次内容 上次研究了 一行赋值多个变量 a = b = 5 a, b = 7, 8 还研究了 标识符的惯用法 python使用的是 snake_case蛇形命名法 用下划线 分隔开小 ...
- [rCore学习笔记 013]GDB跟踪程序
题目要求 请学习 gdb 调试工具的使用(这对后续调试很重要),并通过 gdb 简单跟踪从机器加电到跳转到 0x80200000 的简单过程.只需要描述重要的跳转即可,只需要描述在 qemu 上的情况 ...
- FFmpeg开发笔记(四十)Nginx集成rtmp模块实现RTMP推拉流
<FFmpeg开发实战:从零基础到短视频上线>一书的"10.2.2 FFmpeg向网络推流"介绍了轻量级流媒体服务器MediaMTX,虽然MediaMTX使用很简单, ...
- Mysql函数1-IFNULL
IFNULL函数用于判断参数值是null时则返回指定内容. 原本 select goods_base_name,goods_id from goods where goods_id in (6,7,8 ...
- 如何通过PowerShell批量修改O365用户的office phone属性值
我的博客园:https://www.cnblogs.com/CQman/ 如何通过PowerShell批量修改O365用户的office phone属性值? 需求信息: 组织中的O365用户在创建时, ...
- EF Core性能优化技巧
代码层面的优化 1. 使用实例池 EFCore2.0 为DbContext引入新的注册方式:透明地注册了 DbContext实例池,使用这种方式可以避免始终创建新的实例,EF Core 将重置其状态并 ...
- 【Hibernate】05 缓存与MySQL事务隔离
Cache 什么是缓存? 数据存储到数据库,是从内存中以流的方式写进[输出]到数据库,其效率并不是很高 - 所以在内存中暂存一部分数据,可以不以流的方式读取,效率是非常高的[相对于流来说] Hiber ...
- ROS(机器人操作系统)的基本了解
参考: https://blog.csdn.net/qq_51963216/article/details/125754175 https://zhuanlan.zhihu.com/p/5956062 ...
- 如何使用二阶优化算法实现对神经网络的优化 —— 分布式计算的近似二阶优化算法实现对神经网络的优化 —— 《Distributed Hessian-Free Optimization for Deep Neural Network》
论文: <Distributed Hessian-Free Optimization for Deep Neural Network> 地址: https://arxiv.org/abs/ ...
- LeetCode 上1769号 面试编程题,python编程
原题地址: https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/ - ...