在客户端 Header C# 中设置基本身份验证
Setting Basic Auth in client Header C#
我正在尝试在我的代码中设置基本身份验证 header
using (var client = new HttpClient())
{
string username = "6B562D6XXXXAC1A58E5E474B2107FFDAA66EBE94";
string password = "FF8AFB9XXXX331CD0DF0A0C38D6ACD8502224764";
var uri = "https://xxxxxxx/token";
string svcCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", svcCredentials);
}
这将引发以下异常
System.FormatException: 'The format of value 'Bearer Token System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox
1[System.String,System.Runtime.CompilerServices.IAsyncStateMachine]' is invalid.'
我尝试了一些变体,但运气不佳。
试试这个函数TryAddWithoutValidation
:
类似于:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", svcCredentials);
我正在尝试在我的代码中设置基本身份验证 header
using (var client = new HttpClient())
{
string username = "6B562D6XXXXAC1A58E5E474B2107FFDAA66EBE94";
string password = "FF8AFB9XXXX331CD0DF0A0C38D6ACD8502224764";
var uri = "https://xxxxxxx/token";
string svcCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", svcCredentials);
}
这将引发以下异常
System.FormatException: 'The format of value 'Bearer Token System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1+AsyncStateMachineBox
1[System.String,System.Runtime.CompilerServices.IAsyncStateMachine]' is invalid.'
我尝试了一些变体,但运气不佳。
试试这个函数TryAddWithoutValidation
:
类似于:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", svcCredentials);