Search code examples
asp.netasp.net-web-api2asp.net-identityclaimsthinktecture-ident-model

Asp.Net Web Api 2 - How to consume JWT access token and user claims using Identity Model


I have implemented an authorization server in a Asp.Net Web Api project as explained in this article.

Now I need to consume the service from a .Net c# client. In the IdentityModel documentation I can see below example:

var client = new TokenClient(
    "https://server/token",
    "client_id",
    "secret");

var response = await client.RequestClientCredentialsAsync("scope");
var token = response.AccessToken;

Questions:

  1. What is the purpose of having client Id and a client secret?
  2. How a user will be authenticated using user credentials?
  3. How can I access the user claims in client side?
  4. What is Scope and what is the use of it?

Solution

  • By using IdentityModel.Client; the token can be consumed in following way.

     var client = new TokenClient(authenticationUrl);
     client.Timeout = TimeSpan.FromSeconds(60);
     var tokenResponse = await client.RequestResourceOwnerPasswordAsync(userName, password);
     var handler = new JwtSecurityTokenHandler();
     var token = handler.ReadJwtToken(tokenResponse.AccessToken);
    

    in the token itself contains claim properties.