C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,625 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi All,
How to use Basic Auth with HttpClient?
I use snippet codes below, but keeps getting 401, what I have missed?
var authenticationString = $"{userName}:{userPassword}";
var base64String = Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(authenticationString));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64String);
await _httpClient.GetAsync(url);