Search code examples
c#asp.net-mvcidentitymodel

Request Access Token using refresh token with IdentityModel 4.1.1


I am using IdentityModel 4.1.1 for OAuth2.0 Now here I stucked during create instance of TokenClient, which is used to request new access token using refresh token.

Here's the my code what I am doing,

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);

Below TokenClient Class provided by IdentityModel4.1.1 pkg,

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));
}

As above class represent need to pass HttpMessageInvoker as first parameter, but I fully blanked about it. I also referred IdentityModel document, but didn't get any clue


Solution

  • You have to pass as an argument an HttpClient, with an already set up BaseUrl.

    Take a look at the tests from GitHub, to see how it is constructed.

    Edit: To clear up any confusion, HttpClient has HttpMessageInvoker as a base class.