0

I am working on a project, which need get network access through proxy that require authentication, they way they do that is asking Windows domain user credentials.

The way I set up proxy is http://wiki.bitbinary.com/index.php/Active_Directory_Integrated_Squid_Proxy#Kerberos

On the Windows 10 side, I signed in as the same domain proxy asks. In the proxy settings, I set "Use proxy server" as On, put the address and port, and checked "Don't use the proxy server for local(intranet) address.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace HttpClientPostDemo
{
  class Program
  {
    private static readonly HttpClient client = new HttpClient();

    private static string URL = "https://httpbin.org/status/200";

    static async Task Main(string[] args)
    {
      try
      {
        var response = await client.PostAsync(URL,
            new FormUrlEncodedContent(new Dictionary<string, string>()));
        Console.WriteLine(response.StatusCode);
      }
      catch (Exception exception)
      {
        Console.WriteLine(exception.ToString());
      }
      finally
      {
        Console.ReadLine();
      }
    }
  }
}

When I put "use proxy server" to On, my code doesn't work, but Chrome and Egde can access the Internet.

When I put it off. My code works, and browsers work.

The error stack trace when proxy is on is:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at HttpClientPostDemo.Program.<Main>d__2.MoveNext() in Z:\HttpClientPostDemo\HttpClientPostDemo\Program.cs:line 27

I tried the HttpClientHandler { UseDefaultCredentials = true }, but it doesn't work

singer-neu
  • 15
  • 4
  • 1
    Looks like a duplicate question, [HttpClient and using proxy - constantly getting 407](https://stackoverflow.com/questions/29856543/httpclient-and-using-proxy-constantly-getting-407) – AlwaysLearning Aug 19 '19 at 21:55
  • Hi. thank you for the reply! But the difference is the program is supposed to request something token like from Windows, rather than ask/store proxy user name and password. Or this project is supposed to aware Windows credentials, and use it automatically, proxy credentials is tied with Windows credentials – singer-neu Aug 19 '19 at 22:33
  • The protocol is Microsoft Kerberos, and other applications don't ask Microsoft user name and password, the project has to work in the same way... – singer-neu Aug 19 '19 at 22:35
  • Does `client = new HttpClient() { handler = new HttpClientHandler() { Proxy = WebRequest.GetSystemWebProxy() } };` not work for you? It's supposed to return a proxy configured with the Internet Explorer settings of the currently impersonated user. – AlwaysLearning Aug 20 '19 at 02:48
  • Thx! I checked, I wrote ```private static readonly HttpClient client = client = new HttpClient(new HttpClientHandler() { Proxy = WebRequest.GetSystemWebProxy()});``` And still the same stack trace. The main problem is that the project has to aware system credentials rather than clearly ask username and password. If proxy doesn't enforce username/password the code will work. I also tried the URL inside the browser in the same machine, it works so I rule out public Api service's problem. I am working on .NET Framework project, and I am thinking whether anything I missed in config or manifest. – singer-neu Aug 20 '19 at 16:56

0 Answers0