Identityserver4中ResourceOwnerPassword 模式獲取refreshtoken


一、IS4服務端配置

1、配置Client

new Client
                {
                    ClientId = "xamarin",
                    ClientSecrets = { new Secret("123456".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設置為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(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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM