【问题标题】:How to consume JWT access token and user claims using RestSharp如何使用 RestSharp 使用 JWT 访问令牌和用户声明
【发布时间】:2016-04-30 08:39:23
【问题描述】:

我正在使用下面的代码来使用来自 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"
}
  1. 为什么会出现此错误,我该如何解决?
  2. 如何在客户端访问用户名等声明?
  3. 或者,我如何使用身份模型来使用服务?

【问题讨论】:

    标签: c# asp.net-web-api2 jwt restsharp thinktecture-ident-server


    【解决方案1】:

    您在http请求的Header中添加了grant_typeusernamepassword,它应该在Body部分。

    【讨论】:

      【解决方案2】:

      这是一个老问题,你现在可能已经实现了,但这里是对我有用的解决方案

      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);
      

      【讨论】:

        猜你喜欢
        • 2016-05-01
        • 2019-06-22
        • 1970-01-01
        • 2020-07-31
        • 2021-04-30
        • 2020-11-02
        • 2020-09-28
        • 2016-08-11
        • 2019-04-15
        相关资源
        最近更新 更多