IdentityServer4刷新令牌问题

问题描述 投票:0回答:1

有人知道我可以如何请求refresh_token吗?我正在尝试获取access_token时获取refresh_token。目前,我正在获得此输出。

{
“access_token”: “eyJhbGciOiJSU…Zp1eS1WLY2KW2cLvEHhfDfikAYhPEDbAHTvtQu_yBgRsRxhTTPA”,
“refresh_token”: null,
“expires_in”: “3600”,
“token_type”: “Bearer”,
“scope”: “app.api.whatever.full”
}

因此refresh_token为null。现在,我知道您无法使用授予凭证类型的客户端凭证的刷新令牌。因此,我创建了另外两个客户端,一个具有密码的授予类型,另一个具有混合类型的授予类型。我还将两者的范围都设置为脱机访问。请求令牌时,我得到invalid_client。我被困住了,不知道如何解决此问题。这就是我请求令牌的方式:

public async Task GetUserToken(string client_id, string client_secret, string username, string password, string scope)
{
var client = new HttpClient();

var dict = new Dictionary();
dict.Add(“client_id”, client_id);
dict.Add(“client_secret”, client_secret);
dict.Add(“grant_type”, “password”);
dict.Add(“scope”, scope);
dict.Add(“username”, username);
dict.Add(“password”, password);

client.BaseAddress = new Uri($”{Request.Scheme}://{Request.Host.Value}”);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));

var req = new HttpRequestMessage(HttpMethod.Post, “/connect/token”) { Content = new FormUrlEncodedContent(dict) };
var res = await client.SendAsync(req).ConfigureAwait(false);

if (res.IsSuccessStatusCode)
{
return Ok(res.Content.ReadAsStringAsync().Result);
}
return BadRequest();
}

任何人都可以指出我在哪里出问题了吗?

identityserver4 asp.net-core-identity refresh-token
1个回答
0
投票

我遇到了同样的问题-我为客户端配置了资源所有者密码凭证流(AllowGrantTypes = GrantTypes.ResourceOwnerPassword),并且为客户端(AllowOfflineAccess = true)启用了刷新令牌生成。即便如此,在每次访问令牌请求时,也会返回refresh_token字段并带有null值。

结果是,调用方客户端应用程序还需要添加一个附加作用域,以明确告知IdentityServer4它希望发送刷新令牌。除了在请求访问令牌时指定的所有作用域之外,还应在空格分隔的列表中包含offline_access。一旦添加了这个附加作用域,刷新令牌就会开始通过。

在您的特定示例中,可能的编辑可能是:

dict.Add(“scope”, $"{scope} offline_access");

您可以使用this链接阅读IdentityServer4文档中的此要求。有关信息在最后一段中,尤其是在此句子中:

要请求刷新令牌,客户端需要添加令牌请求中的offline_access范围(并且必须被授权以请求该范围)。

© www.soinside.com 2019 - 2024. All rights reserved.