向标头添加授权 [英] Adding authorization to the headers
本文介绍了向标头添加授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue("OAuth2", Contract.AccessToken);
string result = await PostRequest.AuthenticatedGetData(fullUrl, null, authHeaders);
return result;
...
public static async Task<string> AuthenticatedGetData(string url, FormUrlEncodedContent data, AuthenticationHeaderValue authValue)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authValue.Parameter);
HttpResponseMessage response = await client.PostAsync(new Uri(url), data);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
response = await 部分只是继续一个持续的循环,什么也没有发生.知道我做错了什么吗?
The response = await part just continues an ongoing loop and nothing happens. Any ideas what I am doing wrong?
真正的问题是,我如何发送以下标头:
The question really is, how do I send the following header:
Authorization: OAuth2 ACCESS_TOKEN
到外部 web api
to an external web api
推荐答案
此行
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(authValue.Parameter);
将产生这个标题值
Authorization: ACCESS_TOKEN
其中ACCESS_TOKEN
是authValue.Parameter
的值.您想分配您传递的值以获取所需的标头
Where ACCESS_TOKEN
is the value of authValue.Parameter
. You want to assign the value you passed instead to get the required header
client.DefaultRequestHeaders.Authorization = authValue;
会产生
Authorization: OAuth2 ACCESS_TOKEN
这篇关于向标头添加授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文