在OData的授权标头中传递用户名和密码

时间:2019-12-16 09:12:13

标签: asp.net-core odata

如何在Asp.net core 2.2中的OData URL的授权标头中传递用户名和密码

1 个答案:

答案 0 :(得分:1)

使用HTTPClient,您可以在其中设置用于授权的特定标头值:

string username = "bbb";
string password = "abc";
string url = "https://yourOData.com"

HttpClient httpClient = new HttpClient();
httpClient .DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Basic",
    Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes($"{username}:{password}")));

HttpResponseMessage response = await HttpClient.GetAsync(url);

如果您能正常工作,请查看https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/并重构您的代码以不像我那样实例化HttpClient。

相关问题