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

问题描述 投票:0回答:1

我正在尝试在我的代码中设置基本身份验证标头

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.AsyncTaskMethodBuilder1+AsyncStateMachineBox1 [System.String,System.Runtime.CompilerServices.IAsyncStateMachine]的格式无效。'

c# basic-authentication
1个回答
0
投票

尝试使用此功能TryAddWithoutValidation

类似:

client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", svcCredentials);
© www.soinside.com 2019 - 2024. All rights reserved.