Asp.Net Web Api 2 - 如何使用身份模型使用JWT访问令牌和用户声明

时间:2016-01-25 06:15:43

标签: asp.net asp.net-web-api2 asp.net-identity claims thinktecture-ident-model

我已在Asp.Net Web Api项目中实施了授权服务器,如article中所述。

现在我需要从.Net c#客户端使用该服务。在IdentityModel documentation中,我可以看到以下示例:

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

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

问题:

  1. 让客户端ID和客户端保密的目的是什么?
  2. 如何使用用户凭据对用户进行身份验证?
  3. 如何在客户端访问用户声明?
  4. 什么是Scope以及它的用途是什么?

1 个答案:

答案 0 :(得分:0)

通过using IdentityModel.Client;,可以通过以下方式使用令牌。

 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);
token中的

包含声明属性。