簡體   English   中英

如何使用RestSharp使用JWT訪問令牌和用戶聲明

[英]How to consume JWT access token and user claims using RestSharp

我正在使用以下代碼從Asp.net Web Api 2.2服務中使用JWT訪問令牌。 我已按照本文在Web Api服務中設置授權服務器。 我在客戶端使用RestSharp。

客戶代碼:

            var client = new RestClient(http://localhost:58030);
            client.Timeout = 30000;
            var request = new RestRequest(@"/Oauth/Token", Method.POST);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddHeader("grant_type", "password");
            request.AddHeader("username", "admin@example.com");
            request.AddHeader("password", "Admin@456");
            IRestResponse response = client.Execute(request);
            result = response.Content;

結果我得到以下錯誤:

{
  "error_description":"grant type not supported",
  "error":"unsupported_grant_type"
}
  1. 為什么我收到此錯誤,我該如何解決?
  2. 如何訪問客戶端中的用戶名等聲明?
  3. 或者,我如何使用Identity Model來使用服務?

您將grant_typeusernamepassword添加到http請求的Header中,它應該在Body部分中。

這是一個老問題,你現在可能已經實現了,但是這里的fwiw是適合我的解決方案

var client = new RestClient("http://blahblah/");
var request = new RestRequest("/token", Method.POST);
// all parameters need to go in body as string
var encodedBody = string.Format("username={0}&password={1}&grant_type=password", model.Username, model.Password);
request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);
request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
var response = client.Execute(request);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM