.net-coreidentityserver4asp.net-core-3.1identitymodel

Using ResourceOwnerPassword flow for .NET Core 3.1/IdentityModel 5.1


I am playing with the IdentityServer4. Part of that I am trying to build a client using IdentityModel 5.1.0 and trying to use following piece of code available here

// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice", "password", "api1");

if (tokenResponse.IsError)
{
    Console.WriteLine(tokenResponse.Error);
    return;
}

Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");

But this is giving me following error.

error CS1729: 'TokenClient' does not contain a constructor that takes 3 arguments

From the docs, it looks like that page is only applicable to Core 1.0. When I change the documentation to 3.1.0, I get

Sorry This pages does not exist yet

Does this mean that ResourceOwnerPassword flow is not supported for the .NET Core 3.1?


Solution

  • Ctrl + clicking on the method takes to to its signature, where you can find out the specific parameters that the method expects.

    Browsing the repo, I've found this snippet on using the password credentials token request:

    var response = await _client.RequestPasswordTokenAsync(new PasswordTokenRequest
    {
        ClientId = "client",
        UserName = "user",
        Password = "password",
        Scope = "scope",
        Resource = { "resource1", "resource2" }
    });
    

    another overload:

    var response = await tokenClient.RequestPasswordTokenAsync(userName: "user", password: "password", scope: "scope");
    

    Or see the actual method definition or another helper.

    A useful tip: popular packages usually have a lot of tests. You can check them out to learn how to use the library.