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