我需要在HttpClient中添加两个Authorization标头,如下所示:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "XYZNQVJJTkFQUDpX...=");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwToken);
但是似乎最后一个会覆盖第一个。
我需要基本和不记名令牌。用于我的承载令牌可以通过承载SAP WebService的代理服务器(SAP服务器的基本令牌)传递。在这种情况下,我该怎么办?
更新:
如何编写2个HttpRequestMessage?
string webServiceUrl = "https://adfs.xxx.xxx/";
string strURL = "https://xxx.xxx.xxx/";
HttpResponseMessage responseMessage;
HttpClient client = new HttpClient();
//--1st HttpRequestMessage
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, strURL);
tokenRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", "XYZNQVJJTkFQUDpX...=");
HttpContent httpContent = new FormUrlEncodedContent(
new[]
{
new KeyValuePair<string, string>("grant_type", "xxx"),
new KeyValuePair<string, string>("client_id", "xxx"),
new KeyValuePair<string, string>("scope", "xxx"),
new KeyValuePair<string, string>("assertion", Base64Assertion)
});
tokenRequest.Content = httpContent;
var tokenResponseMessage = await client.SendAsync(tokenRequest);
var token = await tokenResponseMessage.Content.ReadAsStringAsync();
//-- 2nd HttpRequestMessage
var serviceRequest = new HttpRequestMessage(HttpMethod.Get, webServiceUrl);
serviceRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var serviceResponseMessage = await client.SendAsync(serviceRequest);
答案 0 :(得分:0)
它必须通过HTTP做更多的事情。不可能通过多个Authentication headers
发送答案 1 :(得分:0)
在这种情况下,我该怎么办?
您将需要两个单独的客户端,每个客户端都有各自的默认授权标头
client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "XYZNQVJJTkFQUDpX...=");
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwToken);
或一个没有默认值且根据请求设置了授权的客户端。
例如
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, authServerUrl);
tokenRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", "XYZNQVJJTkFQUDpX...=");
var httpContent = new FormUrlEncodedContent(
new[]
{
new KeyValuePair<string, string>("grant_type", "xxx"),
new KeyValuePair<string, string>("client_id", "xxx"),
new KeyValuePair<string, string>("scope", "xxx"),
new KeyValuePair<string, string>("assertion",Base64Assertion)
});
tokenRequest.Content = httpContent;
var tokenResponseMessage = await client.SendAsync(tokenRequest);
var token = await responseMessage.Content.ReadAsStringAsync();
var serviceRequest = new HttpRequestMessage(HttpMethod.Get, webServiceUrl);
serviceRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var serviceResponseMessage = await client.SendAsync(serviceRequest);
//...