IdentityServer4中ResourceOwnerPassword模式获取accecc_token,并使用refresh_token刷新accecc_token
一、IS4服务端配置
1、配置Client
new Client
{
ClientId = "xamarin",
ClientSecrets = { new Secret("secret".Sha256()) },
AccessTokenLifetime = ,//设置AccessToken过期时间
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
RefreshTokenExpiration = TokenExpiration.Absolute,
AbsoluteRefreshTokenLifetime = ,
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()
.AddDeveloperSigningCredential()
.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
注意:
- grant_type改为refresh_token,表明刷新token
- username与password不需要带了
- 添加参数refresh_token,值为获取accecc_token时的refresh_token值

原文地址:https://www.wandouip.com/t5i43236/
IdentityServer4中ResourceOwnerPassword模式获取accecc_token,并使用refresh_token刷新accecc_token的更多相关文章
- Identityserver4中ResourceOwnerPassword 模式获取refreshtoken
一.IS4服务端配置 1.配置Client new Client { ClientId = "xamarin", ClientSecrets = { ".Sha256() ...
- identityserver4 (ids4)中如何获取refresh_token刷新令牌token 使用offline_access作用域
ids4默认自带的api接口/api/connect/token 调用这个接口的时候,需要在body里面的 x-www-form-urlencoded模式下写 { grant_type: &q ...
- IdentityServer4中文文档
欢迎IdentityServer4 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架. 它在您的应用程序中启用以下功能: 认证即服务 ...
- Core篇——初探IdentityServer4(客户端模式,密码模式)
Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...
- IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端
IdentityServer4 中文文档 -15- (快速入门)添加 JavaScript 客户端 原文:http://docs.identityserver.io/en/release/quicks ...
- IdentityServer4 自定义授权模式
IdentityServer4除了提供常规的几种授权模式外(AuthorizationCode.ClientCredentials.Password.RefreshToken.DeviceCode), ...
- MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- 深入理解JavaScript中创建对象模式的演变(原型)
深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...
- MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
随机推荐
- PHP ftp_size() 函数
定义和用法 ftp_size() 函数返回 FTP 服务器上指定文件的大小. 该函数以字节返回指定文件的大小,如果出错则返回 -1. 语法 ftp_size(ftp_connection,file) ...
- PHP substr_replace() 函数
实例 把 "Hello" 替换成 "world": <?php高佣联盟 www.cgewang.comecho substr_replace(" ...
- bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...
- 区块链钱包开发 - USDT - 二、创建交易错误以及解决方法
这里总结了开发中一些常见报错和解决方案 1. 提示:createRawtx_change "Amount is not a number" 解决:参数中 tx 的 amout 需要 ...
- 《Head First 设计模式》:单件模式
正文 一.定义 单件模式确保一个类只有一个实例,并提供一个全局访问点. 要点: 定义持有唯一单件实例的类变量. 私有化构造,避免其他类产生实例. 对外提供获取单件实例的静态方法. 二.实现步骤 1.创 ...
- 好用的连接池-druid
druid连接池是阿里巴巴的数据库连接池项目.它的一个亮点强大的监控功能以及防SQL注入,同时不影响性能.这里是它的GitHub地址.感觉druid扩展的功能还是很实用的. 实用的功能 详细的监控 E ...
- Nginx一个server主机上80、433,http、https共存
如果一站点既要80 http访问,又要443https访问. 要让https和http并存,不能在配置文件中使用ssl on,配置listen 443 ssl; 实例 server { listen ...
- 程序员必须了解!IntelliJ IDEA 2020.2的新增功能
IDEA 因为之前破解过,所以家里的电脑都是19版本的,用的也比较顺手,也就一直懒得去动他,但是,程序猿的好奇心可能真的挺重,猎奇心里,在网上也看到了很多关于2020版本的idea的各种好处,于是,闲 ...
- SSH config语法关键字
1.SSH config 语法关键字 host 别名 HostName 主机名(ip) User 用户(root就是一个用户) Port 端口(默认22) IdentityFile 密钥文件的路 ...
- Vue 修饰符sync的应用
官方链接 https://cn.vuejs.org/v2/guide/components-custom-events.html#sync-修饰符 这个解释有点不太直观,用代码解释一下 父组件 v-b ...