Search code examples
asp.net-coreasp.net-web-apiidentityserver4identitymodel

What has replaced RequestResourceOwnerPasswordAsync in IdentityModel v.4.3.0?


I'm trying to get the resource owner information, but the RequestResourceOwnerPasswordAsync method is not available in the TokenClient class in v4.3.0. I've searched the documentation, but haven't found the replacement for this method. The following is my code:

enter image description here


Solution

  • You can use RequestPasswordTokenAsync: Sends a token request using the password grant type.

    I believe the recommended way is to use the HttpClientFactory:

    //private readonly IHttpClientFactory _httpClientFactory;
    
    var client = _httpClientFactory.CreateClient();
    var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
    
    if (disco.IsError) throw new Exception(disco.Error);
    
    var tokenClient = _httpClientFactory.CreateClient();
    
    var tokenResult = tokenClient.RequestPasswordTokenAsync(new PasswordTokenRequest
        {
            Address = disco.TokenEndpoint,
            ClientId = "ro.client",
            ClientSecret = "secret",
            UserName = "alice",
            Password = "alice"
        });