我正在使用以下代码从Asp.net Web Api 2.2服务中使用JWT访问令牌。我已按照this article在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"
}
答案 0 :(得分:2)
您已将grant_type
,username
和password
添加到http请求的Header
,它应位于Body
部分。
答案 1 :(得分:-1)
这是一个老问题,你现在可能已经实现了,但是这里的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);