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:
Asked
Active
Viewed 2,207 times
3 Answers
5
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"
});
-
Thanks for your response. Just to make sure I'm understanding you correctly, your solution will address the resource owner password grant type? – user1647160 Jun 19 '20 at 15:51
-
@user1647160 Yes. _OAuth 2.0 resource owner password credential grant (aka password)_ As you can see you can send the username, password in the request. – Jun 19 '20 at 17:10
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

nahidf
- 2,260
- 1
- 15
- 22
-
Thanks for your response. Just to make sure I'm understanding you correctly, your solution will address the resource owner password grant type? – user1647160 Jun 19 '20 at 15:51
-
yes. read more here: https://tools.ietf.org/html/rfc6749#section-4.3 & https://www.oauth.com/oauth2-servers/access-tokens/password-grant/ – nahidf Jun 19 '20 at 18:22
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" />

VivekDev
- 20,868
- 27
- 132
- 202