我正在尝试更新令牌,但是IdentityServer4遇到一些问题。
以下是我的代码。
private async Task<string> RenewTokens()
{
/// get the current HttpContext to access the tokens
HttpContext httpContext = _httpContextAccessor.HttpContext;
DiscoveryResponse discoveryResponse = await _httpClient.GetDiscoveryDocumentAsync(Constants.IDP_URL);
/// refresh the tokens
TokenResponse tokenResponse = await _httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest
{
Address = discoveryResponse.TokenEndpoint,
ClientSecret = Credentials.CLIENT_SECRET,
ClientId = Credentials.CLIENT_ID,
RefreshToken = "refreshed",
});
if (!tokenResponse.IsError)
{
List<AuthenticationToken> updatedTokens = new List<AuthenticationToken>
{
new AuthenticationToken
{
Name = OpenIdConnectParameterNames.IdToken,
Value = tokenResponse.IdentityToken
},
new AuthenticationToken
{
Name = OpenIdConnectParameterNames.AccessToken,
Value = tokenResponse.AccessToken
},
new AuthenticationToken
{
Name = OpenIdConnectParameterNames.RefreshToken,
Value = tokenResponse.RefreshToken
}
};
DateTime expiresAt = DateTime.UtcNow + TimeSpan.FromSeconds(tokenResponse.ExpiresIn);
updatedTokens.Add(new AuthenticationToken
{
Name = "expires_at",
Value = expiresAt.ToString("o", CultureInfo.InvariantCulture)
});
/// get authenticate result containing the current principal and properties
AuthenticateResult authenticationResult = await httpContext.AuthenticateAsync("cookies");
authenticationResult.Properties.StoreTokens(updatedTokens);
await httpContext.SignInAsync("cookies", authenticationResult.Principal, authenticationResult.Properties);
/// return the new access token
return tokenResponse.AccessToken;
}
return string.Empty;
}
当我点击RequestRefreshTokenAsync时,结果如下。
System.ArgumentException:必填参数(参数'refresh_token') 在IdentityModel.Internal.DictionaryExtensions.AddRequired(IDictionary`2 字典,字串键,字串值,布尔allowEmpty) 在IdentityModel.Client.HttpClientTokenRequestExtensions.RequestRefreshTokenAsync(HttpMessageInvoker 客户端,RefreshTokenRequest请求,CancellationToken cancelToken)
我尝试添加RefreshTokenRequest对象
Parameters = new Dictionary<string, string>
{
{ "grant_type", "refresh_token" }
}
但还是没有运气,它未能说明“ grant_type”是重复密钥。
我的最新更新
TokenResponse tokenResponse = await _httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest
{
Address = discoveryResponse.TokenEndpoint,
ClientSecret = Credentials.CLIENT_SECRET,
ClientId = Credentials.CLIENT_ID,
RefreshToken = "refreshed"
});
但tokenResponse无效
{“错误”:“ invalid_grant”}
并设置
GrantType = "refresh_token"
仍然没有运气。
我想念什么?
https://readthedocs.org/projects/identitymodel/downloads/pdf/latest/
答案 0 :(得分:0)
以下流程支持刷新令牌:授权代码,混合和资源所有者密码凭据流程。
http://docs.identityserver.io/en/latest/topics/refresh_tokens.html