此身份持有者令牌未被识别

This Identity Bearer token is not being recognized

我正在按照 this 教程学习使用 Identity。很有帮助的是,作者在他自己的服务器上提供了他的代码的工作示例。我制作了这个执行以下操作的控制台应用程序:

  1. 注册一个帐户。
  2. 为此帐户检索令牌。
  3. 使用此令牌检索信息。

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    
    namespace IdentityConsoleApp
    {
    class Program
    {
    static void Main(string[] args)
    {
        string userName = "john4";
        string password = "Password@123";
        var registerResult = Register(userName, password);
    
        Console.WriteLine("Registration Status Code: {0}", registerResult);
    
        string token = GetToken(userName, password);
        Console.WriteLine("");
        Console.WriteLine("Access Token:");
        Console.WriteLine(token);
    
        Console.WriteLine("");
        Console.WriteLine(GetOrders(token));
    
        Console.Read();
    }
    
    
    static string Register(string name, string apassword)
    {
        var registerModel = new
        {
            userName = name,
            password = apassword,
            confirmPassword = apassword
        };
        using (var client = new HttpClient())
        {
            var response =
                client.PostAsJsonAsync(
                " http://ngauthenticationapi.azurewebsites.net/api/account/register",
                registerModel).Result;
            return response.StatusCode.ToString();
        }
    }
    
    
    static string GetToken(string userName, string password)
    {
        var pairs = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>( "grant_type", "password" ), 
                        new KeyValuePair<string, string>( "userName", userName ), 
                        new KeyValuePair<string, string> ( "password", password )
                    };
        var content = new FormUrlEncodedContent(pairs);
        using (var client = new HttpClient())
        {
            var response =
                client.PostAsync(" http://ngauthenticationapi.azurewebsites.net/token", content).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }
    
    
    static string GetOrders(string token)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    
            var response = client.GetAsync(" http://ngauthenticationapi.azurewebsites.net/api/Orders").Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }
       }
     }
    

此代码很容易复制并粘贴到 Visual Studio 中的新 C# 控制台项目中。只需将 "John4" 替换为任何尚未使用的随机用户名即可。

我得到以下输出:

Registration status Code: OK

Access Token: {[Access token content]}

{"message": "Authorization has been denied for this request"}

假设教程作者的软件没有问题,为什么我无法通过bearertoken授权?我传递的令牌有什么问题?

在您的代码中,函数 GetToken 的响应将 return JSON 对象不仅 access_token 属性,所以您应该提取 access_token 字段然后将其发送到端点,您当前从 GetToken 收到的响应如下所示:

{
"access_token": "pXuyMK2GmuffgCTJJrFDBsJ_JqJ0qkIkEePhswVSjIv-A35OB7WoFxiYGg-WdjyCEonEjtmcondVTmdZE97T03WQ0agPbwTizdgxYCVE3rPJ9BmqT84M66Z0XXCrYnMj9OYl5SmmzcJpmlQd7v2jGG5WkRvJeOeqy1Ez2boXByo2QFDp5X7TqSokhz1Pvsusa3ot4-wgmpVkF6DTpctzv_gXFhjAPHs7NHFFsm_zuyRRvWKkekmATKg-4QJPlxlIn84BvDxNSgs9gQFH8nNFl37-P5BV4PJY43IC7otxBsgJymATFxdPFblcXb1aGIsnPuhU_Q",
"token_type": "bearer",
"expires_in": 1799,
"as:client_id": "",
"userName": "Taiseer",
".issued": "Thu, 05 Mar 2015 20:34:16 GMT",
".expires": "Thu, 05 Mar 2015 21:04:16 GMT"
}