首页
视频
资源
登录
原
.net core 3.1 Identity Server4 (自定义模式)
4820
人阅读
2021/1/18 13:28
总访问:
2590450
评论:
0
收藏:
0
手机
分类:
Ids4
![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 3.1 Identity Server4 (自定义模式) [TOC] ![](https://img.tnblog.net/arcimg/hb/8e4abea9067d4157944d80e90497ace8.png) tn>IdentityServer4除了提供常规的几种授权模式外(AuthorizationCode、ClientCredentials、Password、RefreshToken、DeviceCode),还提供了可以拓展的授权模式,下面就根据源码简单说下IdentityServer4是如何实现自定义授权模式的。 >### 创建CustomAuthCodeValidator类 ![](https://img.tnblog.net/arcimg/hb/a2ea4da918da4d86936094a89c2ae4cc.png) tn>`IExtensionGrantValidator`接口实现了自定义验证的扩展。接着我们通过编写简单的源码实现验证逻辑。 ```csharp public class CustomAuthCodeValidator : IExtensionGrantValidator { private readonly UserManager<IdentityUser> _userManager; public CustomAuthCodeValidator(UserManager<IdentityUser> userManager) { _userManager = userManager; } public string GrantType => "auth_custom"; public async Task ValidateAsync(ExtensionGrantValidationContext context) { var name = context.Request.Raw["name"]; var age = context.Request.Raw["age"]; // 创建无效授权的返回实例 var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant); // 判断name参数与age参数是否为空 if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(age)) { // 赋予无效授权结果 context.Result = errorValidationResult; return; } if (name!="bob" && age!="19") { // 赋予无效授权结果 context.Result = errorValidationResult; return; } // 获取到有效的用户 subject_id var user = await _userManager.FindByNameAsync(name); // 返回有效结果 context.Result = new GrantValidationResult(user.Id, GrantType); } } ``` >### 在Startup.cs中实现自定义验证 ```csharp .AddExtensionGrantValidator<CustomAuthCodeValidator>() ``` ![](https://img.tnblog.net/arcimg/hb/484f69fdb9e74169921cb1cddb7acf01.png) >### 输入相对应的请求参数进行测试 ![](https://img.tnblog.net/arcimg/hb/52f4a1d628f4473f862264babb9485c1.png)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术