问题描述
我尝试了不同的方式来传递header
和content
,但是每次从bad request
获取docusign
来获取访问令牌。
下面是回调Web API方法,该方法在请求authorization code
之后被调用:
[HttpGet]
[AllowAnonymous]
[Route("CallBack")]
public string Callback()
{
string accesstoken = "";
try
{
var response = Request.Query;
if (Request.Query != null && Request.Query.Keys != null && Request.Query.Keys.Count > 0)
{
var authorizationCode = Request.Query["code"];
/*
Request => POST https://account-d.docusign.com/oauth/token
Content-Type: application/x-www-form-urlencoded
Header => Authorization: Basic BASE64_COMBINATION_OF_INTEGRATOR_AND_SECRET_KEYS
Data => grant_type=authorization_code&authorization_code=YOUR_AUTHORIZATION_CODE
*/
//Body
var keyvalues = new Dictionary<string,string>();
keyvalues.Add("grant_type","authorization_code");
keyvalues.Add("authorization_code",authorizationCode);
var bodyContent = new FormUrlEncodedContent(keyvalues);
//Header
//Content-Type: application/x-www-form-urlencoded
//Authorization: Basic BASE64_COMBINATION_OF_INTEGRATOR_AND_SECRET_KEYS
string base64Decoded = configuration.IntegrationKey + ":" + configuration.SecretKey;
string base64Encoded;
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Decoded);
base64Encoded = System.Convert.ToBase64String(data);
var client = new System.Net.Http.HttpClient();
client.BaseAddress = new Uri("https://account-d.docusign.com");
var request = new System.Net.Http.HttpRequestMessage(HttpMethod.Post,"https://account-d.docusign.com/oauth/token");
request.Content = bodyContent;
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
request.Headers.Authorization = new AuthenticationHeaderValue("Basic",base64Encoded);
var accesstokenresponse = client.SendAsync(request).Result;
//Set access token
//accesstoken
}
}
catch (Exception ex)
{
}
return accesstoken;
}
解决方法
一些适合您的想法:
- 最好使用OAuth2客户端库。这是InfoSec专家和DocuSign的高度建议。
- 如果您决定采用自己的实现方式:请通过Postman或类似工具尝试OAuth2流程,以确保您确切了解该流程的工作原理。
- 请记住,授权码是有时间限制的。您需要在一分钟左右的时间内将其交换为访问令牌(可能更少,我没有确切的数据)。
- 使用wireshark或类似工具来准确查看要发送到DocuSign的内容。
- 重要的InfoSec问题:在
state
参数中发送一个现时值,然后在从DocuSign收到第一个响应时检查它是否相同。