【问题标题】:Request Access Token using refresh token with IdentityModel 4.1.1使用带有 IdentityModel 4.1.1 的刷新令牌请求访问令牌
【发布时间】:2021-09-25 00:27:12
【问题描述】:

我将 IdentityModel 4.1.1 用于 OAuth2.0 现在在这里我在创建 TokenClient 实例时卡住了,该实例用于使用刷新令牌请求新的访问令牌。

这是我正在做的代码,

TokenClientOptions clientOptions = new TokenClientOptions();
clientOptions.ClientId = _configDetails.Where(x => x.Key == "ClientId").Select(x => x.Value).FirstOrDefault().ToString();
clientOptions.ClientSecret = _configDetails.Where(x => x.Key == "ClientSecret").Select(x => x.Value).FirstOrDefault().ToString();

//Create token client object object
var tokenClient = new TokenClient("?",clientOptions); //Here I need help what I have to pass as first parameter?


TokenResponse refereshtokenCallResponse = await tokenClient.RequestRefreshTokenAsync(token.RefreshToken);

在IdentityModel4.1.1 pkg提供的TokenClient类下,

public class TokenClient
{
    public TokenClient(HttpMessageInvoker client, TokenClientOptions options);
    public TokenClient(Func<HttpMessageInvoker> client, TokenClientOptions options);

    public Task<TokenResponse> RequestAuthorizationCodeTokenAsync(string code, string redirectUri, string codeVerifier = null, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
    public Task<TokenResponse> RequestClientCredentialsTokenAsync(string scope = null, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
    public Task<TokenResponse> RequestDeviceTokenAsync(string deviceCode, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
    public Task<TokenResponse> RequestPasswordTokenAsync(string userName, string password = null, string scope = null, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
    public Task<TokenResponse> RequestRefreshTokenAsync(string refreshToken, string scope = null, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
    public Task<TokenResponse> RequestTokenAsync(string grantType, IDictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken));
}

上述类表示需要将 HttpMessageInvoker 作为第一个参数传递,但我对此完全置若罔闻。 我还提到了 IdentityModel document,但没有得到任何线索

【问题讨论】:

    标签: c# asp.net-mvc identitymodel


    【解决方案1】:

    您必须将 HttpClient 作为参数传递,并带有已设置的 BaseUrl。

    看看GitHub 的测试,看看它是如何构造的。

    编辑:为了消除任何混淆,HttpClient 将 HttpMessageInvoker 作为基类。

    【讨论】:

      【解决方案2】:

      代码示例:

      TokenClient tokenClient = new TokenClient(new HttpClient() { BaseAddress = new Uri(tokenEndpoint) }, new TokenClientOptions { ClientId = "clientId", ClientSecret = "clientSecret" });
      

      tokenEndpoint 通常在发现文档中的位置:

      var client = new HttpClient();
      var disco = await client.GetDiscoveryDocumentAsync(authority);
      var tokenEndpoint = disco.TokenEndpoint;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-29
        • 2019-10-13
        • 1970-01-01
        • 1970-01-01
        • 2020-07-12
        • 2016-01-05
        • 2021-12-17
        • 2021-09-01
        相关资源
        最近更新 更多