Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Proxy issues #25745

Closed
IeuanWalker opened this issue Apr 3, 2018 · 6 comments
Closed

Default Proxy issues #25745

IeuanWalker opened this issue Apr 3, 2018 · 6 comments
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@IeuanWalker
Copy link

Default Proxy issues

Our organisation has a proxy and in previous versions on .NET (.net full framework), you can add the following to the web.config and the application will use your system's default proxy.

<system.net>
   <defaultProxy useDefaultCredentials="true" />
</system.net>

I have tried a lot of different options but just cant seem to get anything to work.

string username = "test";
string password = "test";
WebProxy proxy = new WebProxy($"http://{username}:{password}@bluecoat.domain.gov.uk", 8080)
{
    Address = new Uri("http://{username}:{password}@bluecoat.domain.gov.uk:8080"),
    Credentials = new NetworkCredential(username, password)
};
HttpClientHandler clientHandler = new HttpClientHandler
{
    Proxy = proxy,
    UseProxy = true,
    Credentials = new NetworkCredential(username, password)
};
var httpClient = new HttpClient(clientHandler);

Any help would be greatly appreciated.

@wfurt
Copy link
Member

wfurt commented Apr 3, 2018

You should not need user & password in Uri. What version of .Net and OS is failing for you?
If the request failing to authenticate or is it not going to proxy at all?

@davidsh
Copy link
Contributor

davidsh commented Apr 3, 2018

If you simply want to use the default system proxy and need to pass default credentials to that proxy (because the proxy is an authenticated proxy) during HTTP requests, then do this:

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var client = new HttpClient(handler);

// Now use 'client' to do requests.

This assumes that you already have a default system proxy configured on the machine. I.e. on Windows machine you would set up the default system proxy via Internet Explorer settings using 'Automatic' settings or PAC file or explicit settings. For example:

image

@davidsh
Copy link
Contributor

davidsh commented Apr 3, 2018

Also, putting username and password into the Uri itself is a deprecated concept and is ignored in the HTTP stacks of .NET Framework and .NET Core.

WebProxy proxy = new WebProxy($"http://{username}:{password}@bluecoat.domain.gov.uk", 8080)

@weliwita
Copy link

weliwita commented Apr 4, 2018

I'm also having proxy issues with MacOs.

        static void Main(string[] args)
        {
            HttpClientHandler handler = new HttpClientHandler();
            var client = new HttpClient(handler);
            string baseUrl = "http://www.bbc.com";

            try
            {
                HttpResponseMessage res = client.GetAsync(baseUrl).Result;
                if (res.IsSuccessStatusCode)
                {
                    Console.WriteLine("Success");
                }
                else
                {
                    Console.WriteLine("Failed");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

This code doesn't respect proxy settings configured on MacOs. Is default proxy is not used by HttpClient or am I missing some configuration? I have also posted this on stackoverflow https://stackoverflow.com/questions/49653986/dotnet-core-httpclient-doesnt-respect-proxy-settings-on-mac

@wfurt
Copy link
Member

wfurt commented Apr 4, 2018

On OSX You have to "export http_proxy http://proxy.server.com:3128"
curl library we use under the cover does not know how to read system settings.
Future work is tracked in https://github.com/dotnet/corefx/issues/26593

@davidsh
Copy link
Contributor

davidsh commented Apr 13, 2018

@IeuanWalker I think your questions about to enable default credentials use for the default system proxy have been answered above. So, we'll close this issue now.

@davidsh davidsh closed this as completed Apr 13, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

5 participants