什么在 IdentityModel v.4.3.0 中取代了 RequestResourceOwnerPasswordAsync?

What has replaced RequestResourceOwnerPasswordAsync in IdentityModel v.4.3.0?

我正在尝试获取资源所有者信息,但 RequestResourceOwnerPasswordAsync 方法在 v4.3.0 的 TokenClient class 中不可用。我搜索了文档,但没有找到此方法的替代方法。以下是我的代码:

您可以使用 RequestPasswordTokenAsync使用密码授予类型发送令牌请求。

我认为推荐的方法是使用 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"
    });

如其他响应所示,您也可以使用 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

这是我使用的和它的工作原理。

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;

您可能需要考虑添加以下部分或全部 nuget 包。我的csproj文件如下

<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" />