C# 在客户端头C中设置基本身份验证#

C# 在客户端头C中设置基本身份验证#,c#,basic-authentication,C#,Basic Authentication,我试图在我的代码中设置一个基本的Auth头 using (var client = new HttpClient()) { string username = "6B562D6XXXXAC1A58E5E474B2107FFDAA66EBE94"; string password = "FF8AFB9XXXX331CD0DF0A0C38D6ACD8502224764"; var uri = "https://xxxxxxx

我试图在我的代码中设置一个基本的Auth头

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:“值的格式”承载令牌System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1+AsyncStateMachineBox
1[System.String,System.Runtime.CompilerServices.IAsyncStateMachine]“无效。”


我尝试了一些变体,但运气不好。

尝试此函数
tryadd而不进行验证

比如:

client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", svcCredentials);