Identityserver4中ResourceOwnerPassword 模式获取refreshtoken
一、IS4服务端配置
1、配置Client
new Client
{
ClientId = "xamarin",
ClientSecrets = { new Secret("".Sha256()) },
AccessTokenLifetime = ,//设置AccessToken过期时间
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
//RefreshTokenExpiration = TokenExpiration.Absolute,//刷新令牌将在固定时间点到期
AbsoluteRefreshTokenLifetime = ,//RefreshToken的最长生命周期,默认30天
RefreshTokenExpiration = TokenExpiration.Sliding,//刷新令牌时,将刷新RefreshToken的生命周期。RefreshToken的总生命周期不会超过AbsoluteRefreshTokenLifetime。
SlidingRefreshTokenLifetime = ,//以秒为单位滑动刷新令牌的生命周期。
//按照现有的设置,如果3600内没有使用RefreshToken,那么RefreshToken将失效。即便是在3600内一直有使用RefreshToken,RefreshToken的总生命周期不会超过30天。所有的时间都可以按实际需求调整。
AllowOfflineAccess = true,//如果要获取refresh_tokens ,必须把AllowOfflineAccess设置为true
AllowedScopes = new List<string>
{
"api",
StandardScopes.OfflineAccess, //如果要获取refresh_tokens ,必须在scopes中加上OfflineAccess
StandardScopes.OpenId,//如果要获取id_token,必须在scopes中加上OpenId和Profile,id_token需要通过refresh_tokens获取AccessToken的时候才能拿到(还未找到原因)
StandardScopes.Profile//如果要获取id_token,必须在scopes中加上OpenId和Profile
}
}
2、实现IResourceOwnerPasswordValidator接口,自定义用户登录
public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
//根据context.UserName和context.Password与数据库的数据做校验,判断是否合法
if (context.UserName == "test" && context.Password == "test")
{
context.Result = new GrantValidationResult(
subject: context.UserName,
authenticationMethod: OidcConstants.AuthenticationMethods.Password);
}
else
{
//验证失败
context.Result = new GrantValidationResult(
TokenRequestErrors.InvalidGrant,
"invalid custom credential"
);
}
return Task.FromResult();
}
}
3、在Startup中加入如下配置
services.AddIdentityServer()
.AddSigningCredential(IdentityServerBuilderExtensionsCrypto.CreateRsaSecurityKey())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryClients(Config.GetClients())
.AddProfileService<ProfileService>()
.AddResourceOwnerValidator<ResourceOwnerPasswordValidatorService>();//注入自定义用户登录验证
二、客户端获取access_token+refresh_token
如果是后台代码需要获取access_token+refresh_token,则可以参考官方Samples,https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Clients/src/ConsoleResourceOwnerFlowRefreshToken
如果是前端需要获取access_token+refresh_token,则可以通过 http://localhost:5000/connect/token 接口获取
1、获取access_token+refresh_token
获取access_token+refresh_token的参数配置如下,Content-Type的值是 application/x-www-form-urlencoded


2、通过第一步获取到的refresh_token去刷新access_token

Identityserver4中ResourceOwnerPassword 模式获取refreshtoken的更多相关文章
- IdentityServer4中ResourceOwnerPassword模式获取accecc_token,并使用refresh_token刷新accecc_token
一.IS4服务端配置 1.配置Client new Client { ClientId = "xamarin", ClientSecrets = { new Secret(&quo ...
- IdentityServer4中文文档
欢迎IdentityServer4 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架. 它在您的应用程序中启用以下功能: 认证即服务 ...
- Core篇——初探IdentityServer4(客户端模式,密码模式)
Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...
- IdentityServer4 自定义授权模式
IdentityServer4除了提供常规的几种授权模式外(AuthorizationCode.ClientCredentials.Password.RefreshToken.DeviceCode), ...
- IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端
IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端 原文:http://docs.identityserver.io/en/release/quicks ...
- MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- 深入理解JavaScript中创建对象模式的演变(原型)
深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...
- MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- js中State模式的解析及运用
状态模式,在大的范畴中的定义为当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.每种编程语言有不同的实现方式,运用的范围也多用于游戏之中. 这里我用javascript来模拟状 ...
随机推荐
- Spring Boot 如何给微信公众号返回消息
hello 各位小伙伴,今天我们来继续学习如何通过 Spring Boot 开发微信公众号.还没阅读过上篇文章的小伙伴建议先看看上文,有助于理解本文: Spring Boot 开发微信公众号后台 上篇 ...
- xss代码集
</script>"><script>prompt(1)</script> </ScRiPt>"><ScRiPt& ...
- MySQL系统变量auto_increment_increment与auto_increment_offset学习总结
在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量.在官方文档中,将其划分为Replication Mas ...
- 文件I/O——文件打开函数(open/openat)
一.open函数 1.函数原型:int open(const char *path,int oflag,.../* mode_t mode */); 2.头文件:#include <fcntl. ...
- 对pwntools生成的exp模版做了一些修改
安装pwntools后,有一些命令行的工具可以用 ~ pwn template -h usage: pwn template [-h] [--host HOST] [--port PORT] [--u ...
- [考试反思]NOIP模拟测试19:洗礼
[]260 []230[]210 []200[8]170[9]160 这套题一般,数据很弱,T1T2暴力都能A,而且都是一些思维题,想不到就爆0. 原因不明,很多一直很强的人在这一次滑铁卢了,于是我个 ...
- 分布式系统中session一致性问题
业务场景 在单机系统中,用户登陆之后,服务端会保存用户的会话信息,只要用户不退出重新登陆,在一段时间内用户可以一直访问该网站,无需重复登陆.用户的信息存在服务端的 session 中,session中 ...
- 因为 GitHub Actions 我发现了 Jake Wharton 的一个仓库
本文微信公众号「AndroidTraveler」首发. 背景 昨天(2019-11-14)上去 GitHub 上面一看,结果来了个下面的提示: 点进去一看: 看来是自动化构建相关的. 那就试一下,选了 ...
- python中文件的基础操作
打开文件的三种方式: open(r'E:\学习日记\python\code\文件的简单操作.py') open('E:\\学习日记\\python\\code\\文件的简单操作.py') open(' ...
- 大数据HDFS相关的一些运维题
1.在 HDFS 文件系统的根目录下创建递归目录“1daoyun/file”,将附件中的BigDataSkills.txt 文件,上传到 1daoyun/file 目录中,使用相关命令查看文件系统中 ...