此代码导致错误的请求或“任务已取消”因为它包含两种类型的身份验证。
处理程序。UseDefaultCredentials=true代码>
//Create the HttpClientHandler
HttpClientHandler handler = new HttpClientHandler();
//Add the AD Credentials
handler.UseDefaultCredentials = true;
if (Client == null)
{
Client = new HttpClient(handler);
Client.Timeout = TimeSpan.FromMinutes(0.5);
}
Client.DefaultRequestHeaders.Clear();
//Add Basic header credentials for the remote server
var authToken = Encoding.ASCII.GetBytes($"{userName}:{password}");
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
if (ReqHeaders.Count != 0)
{
foreach (KeyValuePair<string, string> pair in ReqHeaders)
{
Client.DefaultRequestHeaders.Add(pair.Key, pair.Value);
}
}
我需要首先对ESB进行本地身份验证,然后对远程服务器进行身份验证。
而不是使用客户端设置授权标头。DefaultRequestHeaders。授权
,尝试直接设置标题:
Client.DefaultRequestHeaders.Add("Authorization", $"Basic {Convert.ToBase64String(authToken)}");
您确定令牌应该是基本64格式吗?