Identityserver4中ResourceOwnerPassword 模式获取refreshtoken
一、IS4服務端配置
1、配置Client
new Client{ClientId = "xamarin",ClientSecrets = { new Secret("secret".Sha256()) },AccessTokenLifetime = 1800,//設置AccessToken過期時間AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,//RefreshTokenExpiration = TokenExpiration.Absolute,//刷新令牌將在固定時間點到期AbsoluteRefreshTokenLifetime = 2592000,//RefreshToken的最長生命周期,默認30天RefreshTokenExpiration = TokenExpiration.Sliding,//刷新令牌時,將刷新RefreshToken的生命周期。RefreshToken的總生命周期不會超過AbsoluteRefreshTokenLifetime。SlidingRefreshTokenLifetime = 3600,//以秒為單位滑動刷新令牌的生命周期。
//按照現有的設置,如果3600內沒有使用RefreshToken,那么RefreshToken將失效。即便是在3600內一直有使用RefreshToken,RefreshToken的總生命周期不會超過30天。所有的時間都可以按實際需求調整。
AllowOfflineAccess = true,//如果要獲取refresh_tokens ,必須把AllowOfflineAccess設置為trueAllowedScopes = new List<string>{"api",StandardScopes.OfflineAccess, //如果要獲取refresh_tokens ,必須在scopes中加上OfflineAccessStandardScopes.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(0);}}?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
?
轉載于:https://www.cnblogs.com/fengchao1000/p/9849806.html
總結
以上是生活随笔為你收集整理的Identityserver4中ResourceOwnerPassword 模式获取refreshtoken的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页编程技术二(块级元素和行内标签)
- 下一篇: 织梦点击数或者其他数值过【千】过【万】过