前言

上一篇讲完了基本使用 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的更多相关文章

  1. Custom Data Service Providers

    Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...

  2. ASP.NET Core3.1使用Identity Server4建立Authorization Server-2

    前言 建立Web Api项目 在同一个解决方案下建立一个Web Api项目IdentityServer4.WebApi,然后修改Web Api的launchSettings.json.参考第一节,当然 ...

  3. ASP.NET Core3.1使用Identity Server4建立Authorization Server

    前言 网上关于Identity Server4的资料有挺多的,之前是一直看杨旭老师的,最近项目中有使用到,在使用.NET Core3.1的时候有一些不同.所以在此记录一下. 预备知识: https:/ ...

  4. [转]How to: Create a Custom Principal Identity

    本文转自:https://msdn.microsoft.com/en-us/library/aa702720(v=vs.110).aspx The PrincipalPermissionAttribu ...

  5. Filters in ASP.NET Core

    Filters in ASP.NET Core allow code to be run before or after specific stages in the request processi ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门

    注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...

  10. ASP.NET Identity 一 (转载)

    来源:http://www.cnblogs.com/r01cn/p/5194257.html 注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的A ...

随机推荐

  1. 重磅消息:微软发布多平台应用UI框架 MAUI,网友直呼:牛x

    本文内容来自微软开发博客:https://devblogs.microsoft.com/dotnet/introducing-net-multi-platform-app-ui/ 转载请注明来源,公众 ...

  2. Nuxt.js 中使用 useHydration 实现数据水合与同步

    title: Nuxt.js 中使用 useHydration 实现数据水合与同步 date: 2024/7/18 updated: 2024/7/18 author: cmdragon excerp ...

  3. 适用于 .NET 的现代化、流畅、可测试的HTTP客户端库

    前言 今天大姚给大家分享一个.NET开源(MIT License).免费.现代化.流畅.可测试.可移植的URL构建器和HTTP客户端库:Flurl. 项目介绍 Flurl是一个集现代性.流畅性.异步性 ...

  4. 深入探究 Golang 反射:功能与原理及应用

    Hi 亲爱的朋友们,我是 k 哥.今天,咱们来一同探讨下 Golang 反射. Go 出于通用性的考量,提供了反射这一功能.借助反射功能,我们可以实现通用性更强的函数,传入任意的参数,在函数内通过反射 ...

  5. 【Java】Applet开发

    一.Applet开发环境准备 IDEA在2018版本默认提供了Applet启动配置 创建一个Applet应用配置: 在IDEA2019版本之后,这个应用配置选项被移除了,改为Java Applet S ...

  6. 【微信小程序】 使用NPM包与VantWeapp

    小程序对npm的支持与限制 目前,小程序中已经支持使用npm安装第三方包,从而来提高小程序的开发效率. 但是,在小程序中使用npm包有如下3个限制: ① 不支持依赖于Node.js内置库的包② 不支持 ...

  7. 【H5】01 入门 & 概述

    前言 看了很多教程资料,很难受,东西讲不全,一些属性就是简单的解释就没了,不能追根问底的了解这个东西,所以在后面, 越是学习就越是费解,出现的问题也越来越多.什么快速学完都是不存在的,培训机构的东西也 ...

  8. python运行报警告:Cython directive 'language_level' not set, using '3str' for now (Py3)

    相关: https://stackoverflow.com/questions/34603628/how-to-specify-python-3-source-in-cythons-setup-py ...

  9. 【官网链接】 REPRODUCIBILITY —— pytorch的可复现性

    官网链接地址: https://pytorch.org/docs/stable/notes/randomness.html ====================================== ...

  10. MindSpore 初探, 使用LeNet训练minist数据集

    如题所述,官网地址: https://www.mindspore.cn/tutorial/zh-CN/r1.2/quick_start.html 数据集下载: mkdir -p ./datasets/ ...