如何使用RestSharp消耗智威汤逊访问令牌和用户要求 [英] How to consume JWT access token and user claims using RestSharp

查看:394
本文介绍了如何使用RestSharp消耗智威汤逊访问令牌和用户要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码消耗从Asp.net的Web API 2.2服务智威汤逊访问令牌。我遵循的这篇文章设置中的Web API服务授权服务器。我使用在客户端RestSharp



客户端代码:

  VAR的客户=新RESTClient实现(HTTP://本地主机:58030); 
client.Timeout = 30000;
变种要求=新RestRequest(@/的Oauth /令牌,Method.POST);
request.AddHeader(内容类型,应用程序/ x-WWW的形式,进行了urlencoded);
request.AddHeader(grant_type,密码);
request.AddHeader(用户名,admin@example.com);
request.AddHeader(密码,管理@ 456);
IRestResponse响应= client.Execute(请求);
结果= response.Content;



结果我收到以下错误:

  {
ERROR_DESCRIPTION:授类型不支持,
错误:unsupported_grant_type
}




  1. 为什么我得到这个错误,我怎么能解决这个问题?

  2. 如何进入索赔,如在客户端的用户名?

  3. 或者我如何使用Identity型消费服务?


解决方案

这是一个老问题,你可能必须通过实施,但现在这里FWIW是为我工作的解决方案

  VAR的客户=新RESTClient实现(HTTP:// blahblah /); 
变种要求=新RestRequest(/标记,Method.POST);
//所有参数都需要身体去为字符串
VAR encodedBody =的String.Format(用户名= {0}&放大器;密码= {1}&安培; grant_type =密码,model.Username, model.Password);
request.AddParameter(应用程序/ x-WWW的形式,进行了urlencoded,encodedBody,ParameterType.RequestBody);
request.AddParameter(内容类型,应用程序/ x-WWW的形式,进行了urlencoded,ParameterType.HttpHeader);
VAR响应= client.Execute(请求);


I'm using below code to consume JWT access token from an Asp.net Web Api 2.2 service. I have followed this article to setup an authorization server in Web Api service. I'm using RestSharp in the client.

client code:

            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;

As a result I'm getting following error:

{
  "error_description":"grant type not supported",
  "error":"unsupported_grant_type"
}

  1. Why I'm getting this error and how can I fix this?
  2. How can I access the Claims such as username in client?
  3. Alternatively how can I use Identity Model to consume the service?

解决方案

It's an old question and you probably have an implementation by now but fwiw here is the solution that worked for me

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

这篇关于如何使用RestSharp消耗智威汤逊访问令牌和用户要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆