博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IdentityServer4问题记录
阅读量:4579 次
发布时间:2019-06-09

本文共 3275 字,大约阅读时间需要 10 分钟。

IdentityServer4

IdentityServer4:2.3.2版本基于数据库的请求token失效

  • 这个问题出现在2.3.2版本内,我去看了下源码,原因如下:
///         /// Adds the validators.        ///         /// The builder.        /// 
public static IIdentityServerBuilder AddValidators(this IIdentityServerBuilder builder) { // core builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); // optional builder.Services.TryAddTransient
(); builder.Services.TryAddTransient
(); return builder; }
  • 重点在这里:

    builder.Services.TryAddTransient<IResourceOwnerPasswordValidator, NotSupportedResourceOwnerPasswordValidator>();

  • NotSupportedResourceOwnerPasswordValidator.cs
public class NotSupportedResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator    {        private readonly ILogger _logger;        ///         /// Initializes a new instance of the 
class. ///
/// The logger. public NotSupportedResourceOwnerPasswordValidator(ILogger
logger) { _logger = logger; } ///
/// Validates the resource owner password credential /// ///
The context. ///
public Task ValidateAsync(ResourceOwnerPasswordValidationContext context) { context.Result = new GrantValidationResult(TokenRequestErrors.UnsupportedGrantType); _logger.LogInformation("Resource owner password credential type not supported. Configure an IResourceOwnerPasswordValidator."); return Task.CompletedTask; } }
  • 可以看到ValidateAsync这里直接返回了UnsupportedGrantType,所以说我们要把这个给下。方法如下:
  • 修改我们的IdentityServer服务器的StartUp.cs:

services.AddIdentityServer()               .AddDeveloperSigningCredential()               // this adds the config data from DB (clients, resources)               .AddConfigurationStore(options =>               {                   options.ConfigureDbContext = builder =>                       builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));               })               // this adds the operational data from DB (codes, tokens, consents)               .AddOperationalStore(options =>               {                   options.ConfigureDbContext = builder =>                       builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));                   // this enables automatic token cleanup. this is optional.                   options.EnableTokenCleanup = true;                   options.TokenCleanupInterval = 30;               }).Services.Replace(ServiceDescriptor.Transient
());
  • 这里我是把IResourceOwnerPasswordValidator的注入服务给替换了一下,用自己的验证逻辑来做。这个验证逻辑参考

转载于:https://www.cnblogs.com/chinalxx/p/10570476.html

你可能感兴趣的文章
java设计模式----工厂方法模式
查看>>
ubuntu下配置安装Myslq
查看>>
phpstudy打不开localhost(Apache)
查看>>
Python实现读取json文件到excel表
查看>>
Opengl_入门学习分享和记录_02_渲染管线(一)顶点着色器&片段着色器
查看>>
[USACO07DEC]Sightseeing Cows
查看>>
什么是网站CDN服务,CDN加速原理?
查看>>
内存泄漏以及常见的解决方法
查看>>
解决itunesconnect无法訪问
查看>>
Java使用OkHttps工具类调用外部接口
查看>>
专题-HTTP
查看>>
寻找“水王”
查看>>
GAT2.0使用文档(组合接口测试)
查看>>
QString 和std::string互转
查看>>
OMAPL138学习----Codec Engine
查看>>
如何写一个完整课堂管理系统(数据库增删改查)
查看>>
HDU1049(水题)
查看>>
python之路 Day8 paramiko学习
查看>>
博客园精华集
查看>>
poj1830:开关问题
查看>>