tg24
tg24

Reputation: 161

Passing Windows Authentication to Proxy using HttpClient

I've trying to pass Windows Authentication to a WebProxy, so that a user doesn't have to type in his login data manually. The use case is a proxy server which checks authentication against a LDAP/AD server, while the users have to change their password periodically.

I've got the following code:

private void button1_ClickAsync(object sender, EventArgs e) {
    Url = "http://local.adress/test";
    Execute();
}

private void button2_Click(object sender, EventArgs e) {
    Url = "https://maps.googleapis.com/maps/api/timezone/json";
    Execute();
}

private void Execute() {
    var handler = new HttpClientHandler();
    handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
    handler.UseDefaultCredentials = true;
    handler.UseProxy = true;
    handler.Proxy = WebRequest.DefaultWebProxy;
    handler.Proxy.Credentials = new NetworkCredential("mydomainuser", "mydomainpassword");
    //handler.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

    var client = new HttpClient(handler);
    Task<string> response = TestConnection(client, Url);
}

private async Task<string> TestConnection(HttpClient client, string url) {
    try {
        using (HttpResponseMessage result = await client.GetAsync(url)) {
            string res = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
            Console.WriteLine("content: " + res);
            return result.ToString();
        }
    } catch (Exception e) {
        Console.WriteLine("error: " + e.Message);
        return e.ToString();
    }
}

When defining the credentials manually (as you can see in the Execute method), everythings works as expected. I've checked the proxy log files to be sure the request is really forwarded through the proxy.

Since it's my goal to spare the user to type in his probably periodically changing password, I've tried to pass the credentials via the CredentialCache.DefaultNetworkCredentials (I've also tried CredentialCache.DefaultCredentials). While executing the request the proxy logs an DENIED and my client returns HTTP error code 407.

Am I missing something obvious? I know there are countless questions on this topic but nothing seems to solve this problem.

Upvotes: 3

Views: 4203

Answers (1)

Jin Thakur
Jin Thakur

Reputation: 2783

You have to define proxy and main URL in code.

var TARGETURL = "http://en.wikipedia.org/";

HttpClientHandler handler = new HttpClientHandler()
{
    Proxy = new WebProxy("http://127.0.0.1:8888"),
    UseProxy = true,
};

try this.

handler.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
handler.Credentials = CredentialCache.DefaultNetworkCredentials;

ok so your webserivces uses windows authentication. Your desktop client is working under your credential you need impersonation https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.impersonate?view=netframework-4.8

check this if it works for you if it is basic authentication.

HttpClient client = new HttpClient(handler);

            **var byteArray = Encoding.ASCII.GetBytes("username:password1234");**


**client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));**

Upvotes: 2

Related Questions