What has replaced RequestResourceOwnerPasswordAsync in IdentityModel v.4.3.0?

Viewed 1763

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

3 Answers

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

This is what I used and its working.

using IdentityModel.Client;
using Microsoft.Extensions.Configuration;
using System.Net.Http;


var tokenClient = new HttpClient();

var tokenResult = await tokenClient.RequestPasswordTokenAsync(new PasswordTokenRequest
{
    Address = _discoveryDocument.TokenEndpoint,
    ClientId = "ro.client",
    ClientSecret = "secret",
    UserName = "Vivek",
    Password = "Vivek"
});
return tokenResult;

You may have to consider adding some or all of the following nuget packages. My csproj file is as follows.

<PackageReference Include="IdentityModel" Version="5.2.0" />
<PackageReference Include="IdentityServer4.Storage" Version="4.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Related