[英]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"
}
您將grant_type
, username
和password
添加到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.