2

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

4

3 回答 3

4

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"
    });
于 2020-06-18T21:03:57.570 回答
2

As other response indicated as well you can use TokenClient - RequestPasswordTokenAsync. Or use as extension for HttpClient . Here is link to documentation: https://identitymodel.readthedocs.io/en/latest/client/token.html#requesting-a-token-using-the-password-grant-type

于 2020-06-19T00:38:00.750 回答
0

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" />
于 2021-11-02T08:27:35.723 回答