sakinala
sakinala

Reputation: 11

Pass the username and password in Authorization headers for OData

How to pass the username and password in Authorization headers for OData URL in Asp.net core 2.2

Upvotes: 0

Views: 448

Answers (1)

citronas
citronas

Reputation: 19365

Use an HTTPClient where you can set the specific header value for authorization:

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);

If you got that one working, take a look at https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ and refactor your code to not instantiate HttpClient in the way I did.

Upvotes: 1

Related Questions