一、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

最新文章

  1. Oracle数据库安装图文操作步骤
  2. Struts2、Spring MVC4 框架下的ajax统一异常处理
  3. mongodb 安装、开启服务 和 php添加mongodb扩展
  4. 测试JdbcTemplate执行SQL语句和存储过程
  5. FTP远程命令集
  6. tar常用解包
  7. 使用solr搭建你的全文检索
  8. ligerUI---下拉菜单(menubar)动态显示(从后台获取数据)
  9. 26 python 初学(线程、同步锁、死锁和递归锁)
  10. Spinner 默认选中
  11. Java学习笔记:多线程(二)
  12. java中函数传值和传地址的问题
  13. CSS命名规范和规则
  14. 高速基于echarts的大数据可视化
  15. java jdbc preparedstatement 分析
  16. Macaca环境搭建全教程
  17. 重构(Refactoring)技巧读书笔记(General Refactoring Tips)
  18. vss安装及服务器端、客户端配置图文教程
  19. Python—文件
  20. ubuntu安装wine

热门文章

  1. Docker在Linux上 基本使用
  2. Spring Boot 使用@Scheduled定时器任务
  3. MySQL在渗透测试中的应用
  4. CSPS模拟 51
  5. Docker的Ubuntu16.04容器如何汉化
  6. Centos7下安装nexus3.x 安装
  7. 『题解』洛谷P3384 【模板】树链剖分
  8. 如何学习python,个人的一些简单见解
  9. web常用自动化库——selenium总结
  10. css3软键盘不盖住输入框的方法