My code is making an HTTP GET to a web service URL that requires basic authentication.
I've implemented this using an HttpClient
with an HttpClientHandler
that has the Credentials
property defined.
This all works perfectly.. Except for one of my use-cases where I'm making the authenticated GET to:
http://somedomain.com
which redirects to http://www.somedomain.com
.
It seems that the HttpClientHandler clears the authentication header during the redirect. How can I prevent this? I want the credentials to be sent regardless of redirects.
This is my code:
// prepare the request
var request = new HttpRequestMessage(method, url);
using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(username, password) , PreAuthenticate = true })
using (var client = new HttpClient(handler))
{
// send the request
var response = await client.SendAsync(request);
Note: this is a related question: Keeping HTTP Basic Authentification alive while being redirected But since I'm using different classes for making the request, there might be a better, more specific solution