I'd like to use HttpClient
and HTTP basic auth by specifying username and password in the URL, like this:
var request = new HttpRequestMessage(HttpMethod.Get,
"https://username:[email protected]");
using (var client = new HttpClient()) {
await client.SendAsync(request);
}
However there is no Authorization
header sent with the request.
Is there a way to tell HttpClient to support this, or do I have to manually fetch the credentials from the URL and set the headers myself?
You need to set the header instead of passing it through the URL
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With