简体   繁体   English

如何将 C# REST API HttpClient 用于基本 Z099FB995346F31C749F6E40C3FZ 身份验证和默认 ADF 凭据?

[英]How do I use C# REST API HttpClient for both Basic header authentication and for default AD credentials?

This code results in a bad request or "A task was canceled."此代码导致错误请求或“任务已取消”。 because it contains both type of authentication.因为它包含两种类型的身份验证。

  1. handler.UseDefaultCredentials = true;
  2. AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
//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);
    }
}

I need to first authenticate locally to the ESB and then to a remote server.我需要先在本地对 ESB 进行身份验证,然后再对远程服务器进行身份验证。

Instead of setting the Authorization header using Client.DefaultRequestHeaders.Authorization , try just setting the header directly:不要使用Client.DefaultRequestHeaders.Authorization设置授权 header ,而是直接设置 header :

Client.DefaultRequestHeaders.Add("Authorization", $"Basic {Convert.ToBase64String(authToken)}");

And are you sure the token should be in base 64 format?你确定令牌应该是 base 64 格式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#:对使用基本身份验证的 api 使用当前凭证 - c#: use current credentials for api that uses basic authentication c#如何在我的HttpClient标头中添加“授权:基本”? - c# How do I add 'Authorization: Basic' to my HttpClient header? 如何通过天蓝色中继C#发送prem wcf https服务的基本身份验证凭据? - How do I send basic authentication credentials for a prem wcf https service over azure relay C#? 如何在c#中使用HttpClient中的凭据? - How to use credentials in HttpClient in c#? 如何将 C# HTTPClient 与 Windows 凭证一起使用 - How to use C# HTTPClient with Windows Credentials 如何从标头中检索基本身份验证凭据? - How can I retrieve Basic Authentication credentials from the header? 如何使用新的WCF REST HttpClient API设置GoogleLogin请求的Authorization标头 - How do I do set the Authorization header of a GoogleLogin request using the new WCF REST HttpClient API 如何删除 HttpClient 请求头 C# 中的默认字符集 - How to remove the default charset in HttpClient Request Header C# 使用C#和httpclient将请求发布到Rest Api - post request to Rest Api with c# and httpclient How do I use SharePoint Rest API in C# .NET Framework using Client ID and Client Secret? - How do I use SharePoint Rest API in C# .NET Framework using Client ID and Client Secret?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM