简体   繁体   English

IdentityModel RequestRefreshTokenAsync 方法始终返回 invalid_client

[英]IdentityModel RequestRefreshTokenAsync method always return invalid_client

I have the following code:我有以下代码:

public async Task<TokenResponse> RefreshTokenAsync(string refreshToken)
{
    HttpClient client = new();
    var discoveryResponse = await client.GetDiscoveryDocumentAsync("https://localhost:44334");

    var response = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
    {
        Address = discoveryResponse.TokenEndpoint,
        ClientId = "...",
        ClientSecret = "...",
        RefreshToken = refreshToken
    });

    return response;
}

And it always returns 400 Bad Request with invalid_client message.它总是返回带有 invalid_client 消息的 400 Bad Request。 When I'm refreshing token in Postman it works well.当我在 Postman 中刷新令牌时,它运行良好。 Where is the problem?问题出在哪儿?

The purpose of the refresh-token is: the user does not need to re-authenticate with the credentials (username/password) in the application every time the session expires. refresh-token的作用是:用户不需要在每次session过期的时候,用应用中的凭据(用户名/密码)重新认证。 So your application needs to connect to the endpoint identity and consume a new refresh token before the token or refresh token times out.因此,您的应用程序需要连接到端点身份并在令牌或刷新令牌超时之前使用新的刷新令牌。 In asp do.net core Identity and JwtToken always have a default timeout value;在 asp do.net core 中,Identity 和 JwtToken 总是有一个默认的超时值; whatever: you need to capture the refresh token before this timeout expires, otherwise your identity understands the user who does not have the browser open or is not online.不管怎样:您需要在此超时到期之前捕获刷新令牌,否则您的身份会理解没有打开浏览器或不在线的用户。 This may imply developing a routine that stays in Roudin-Robin always refreshing the application with the new Token while the browser is open.这可能意味着开发一个保留在 Roudin-Robin 中的例程,在浏览器打开时始终使用新令牌刷新应用程序。

I changed my code to this:我将代码更改为:

public async Task<TokenResponse> RefreshTokenAsync(string refreshToken)
{
    HttpClient client = new();
    var discoveryResponse = await client.GetDiscoveryDocumentAsync("https://localhost:44334");

    var tokenClient = new TokenClient(client, new TokenClientOptions
    {
        Address = discoveryResponse.TokenEndpoint,
        ClientId = "...",
        ClientSecret = "...",
    });

    var response = await tokenClient.RequestRefreshTokenAsync(refreshToken);
    response.HttpResponse.EnsureSuccessStatusCode();

    return response;
}

And now it works as expected.现在它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 始终返回IdentityServer“ invalid_client”错误 - IdentityServer “invalid_client” error always returned PasswordTokenRequest返回invalid_client - PasswordTokenRequest returning invalid_client 使用ASP Identity 2 POST到/ Token端点时始终收到&#39;invalid_client&#39;错误 - Always receiving 'invalid_client' error when POSTing to /Token endpoint with ASP Identity 2 IdentityServer3 PostMan invalid_client - IdentityServer3 PostMan invalid_client Google Adwords OAuth“错误”:“ invalid_client”异常 - Google Adwords OAuth “error” : “invalid_client” exception OAuth {"error":"invalid_client"} grant type "client credential" - C# ASP.Net Web API - OAuth {"error":"invalid_client"} grant type "client credential" - C# ASP.Net Web API ASP.Net MVC 5 SPA模板OWin / token结果为&#39;invalid_client&#39; - ASP.Net MVC 5 SPA Template OWin /token results in 'invalid_client' 带有 OpenID 的 AWS Cognito 因 .NET Core 失败而出现*错误兑换代码:invalid_client* - AWS Cognito with OpenID failing with .NET Core gives error of *Error redeeming code: invalid_client* AWS cognito ASP.NET 核心 MVC 抛出“invalid_client” - AWS cognito ASP.NET Core MVC throws 'invalid_client' Postman 生成的代码在 C# 控制台上获取 {"error":"invalid_client"} - Postman generated code get {"error":"invalid_client"} on C# console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM