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

How to set Proxy Address for Http & Https bindings #1592

Closed
epignosisx opened this issue Oct 11, 2016 · 24 comments
Closed

How to set Proxy Address for Http & Https bindings #1592

epignosisx opened this issue Oct 11, 2016 · 24 comments
Assignees
Milestone

Comments

@epignosisx
Copy link

I'm trying to set a proxy address and credentials to an Https binding but I have not find a way. Do you have some documentation or can you provide a code snippet on how to do it?

@hongdai
Copy link
Contributor

hongdai commented Oct 11, 2016

I don't think custom proxy address is currently supported. The default proxy is used.

Does it block your scenario?

@epignosisx
Copy link
Author

Yes. We have a corporate proxy that is blocking the requests and we have to set the proxy address & credentials (not default) in order to get pass it.

Is there a way to get around this limitation?

@hongdai
Copy link
Contributor

hongdai commented Oct 11, 2016

It's supported to set a non default credential, see the code example https://github.com/dotnet/wcf/blob/master/src/System.Private.ServiceModel/tests/Scenarios/Security/TransportSecurity/Negotiate/NegotiateStream_Http_Tests.4.1.0.cs#L121

The default proxy should work for the machines on your corporate. Could you give a try?

@epignosisx
Copy link
Author

Thanks @hongdai. I tried to configure it like the example you provided but it did not work. I keep getting:

The remote server returned an unexpected response: (407) Proxy Authentication Required

We have always had to specify the proxy address in other apps, but I do not see a way of doing it here. For HttpClient which is what WCF uses under the covers we would need to set the Proxy property:

var httpClientHandler = new HttpClientHandler {
    Proxy = new MyProxy() //MyProxy implements IWebProxy
};

but I do not find an extensibility point to provide the proxy settings.

@zhenlan zhenlan added this to the Future milestone Oct 11, 2016
@mconnew
Copy link
Member

mconnew commented Oct 26, 2016

@epignosisx, can you verify that using HttpClientHandler in that way works for you in your network environment and you can make successful calls? Due to backwards compatibility requirements, some of the properties on HttpClientHandler on some platforms are noop's. If this works for you, then we can add the WCF proxy api to our contract surface and wire the property through. Depending on timing, we might be able to get this done for the 1.1 RTM release (but there's no guarantee so this isn't a hard commitment), otherwise you could use a development build of WCF.

@epignosisx
Copy link
Author

epignosisx commented Oct 27, 2016

I can confirm that assigning an implementation of IWebProxy to HttpClientHandler allows network calls to go through the company proxy. We have a few integrations with external restful services using HttpClient with our proxied HttpClientHandler and works great. We have tested it in Windows 7 & Windows Server 2012 R2.

It would be great if it can make it to 1.1 RTM. This is an impediment for anyone behind a corporate proxy. We had to resort to crafting the SOAP envelope manually.

I can help test a build.

@moliveto
Copy link

moliveto commented Mar 8, 2017

@epignosisx you can provide us an a sample how to set HttpClientHandler to WCF client?

@zhenlan zhenlan mentioned this issue Mar 9, 2017
@zhenlan zhenlan modified the milestones: 2.0, Future Mar 9, 2017
@epignosisx
Copy link
Author

Hi folks. I noticed this got moved from 2.0 to future. This is a pretty big blocker for any customer behind an authenticated proxy. I'm surprised you haven't heard more about this issue from other people.

Is there any chance of considering putting this back on 2.0?

@zhenlan
Copy link
Member

zhenlan commented Apr 14, 2017

Hi @epignosisx this issue is still in 2.0.0 milestone. We will try but depending on how much cycles @mconnew has as the door for 2.0.0 is closing very fast.

@epignosisx
Copy link
Author

Great to hear! Some further ideas on the issue:

It would be great if an application could participate in the creation of the HttpClients. This would not only give us a chance to configure the HttpClientHandler's proxy, but also add additional DelegatingHandlers that we may use. For example to skip the network call and return canned responses (great for mocking external services).

@zhenlan zhenlan modified the milestones: S117, 2.0.0 Apr 18, 2017
@mconnew
Copy link
Member

mconnew commented May 4, 2017

@epignosisx , the limitation we have is we can't add an api which isn't available on the full framework. On the full framework, we use HttpWebRequest so it would be quite a lot of work to make a delegating handler work with HttpWebRequest in a reliable clean way.
It's definitely a nice feature ask and we might be able to accommodate it in the future via some api agnostic mechanism. Off the top of my head, maybe we could add some support for adding something to the BindingParameterCollection used when creating a ChannelFactory. The question though is what? We create a new instance of HttpClientHandler for each remote hostname/credentials combination to prevent any security issues with cached credentials for one request being used for a different request which needs different credentials. This means we need to be able to instantiate the DelegatingHandler inside WCF on demand. There's also the issue that we set a bunch of properties on the HttpClientHandler and there's no guarantee that a pre-constructed DelegatingHandler has an HttpClientHandler at it's root. Also on UWP we don't even use HttpClientHandler and have our own implementation in order to make Certificate authentication work.
So this means we need some kind of factory mechanism where we pass our constructed HttpMessageHandler to which then returns the DelegatingHandler. We could do this by looking for a Func<DelegatingHandler, HttpMessageHandler> in the BindingParamterCollection, but this feels really dirty/hacky from an API design perspective.
Feel free to open an issue on this as a feature request, but supporting additional DelegatingHandler's isn't going to get done for 2.0.0 as it needs a thorough design discussion.

@epignosisx
Copy link
Author

Can't argue with the points you brought up. I didn't account for the Full Framework or UWP. It is definitely tricky to accommodate this feature when there are 3 different ways of making HTTP calls.

I'll open the issue. For now I'm happy with having the ability to specify proxy settings somehow.

@mconnew
Copy link
Member

mconnew commented May 10, 2017

@epignosisx, proxy support has been checked in. It has only had basic manual validation at this point but you should be able to pick up a build with support from the myget dev feeds tomorrow. This only supports running on windows at the moment.

@zhenlan
Copy link
Member

zhenlan commented May 11, 2017

Move the *nix support to post 2.0. @epignosisx please let us know if this is important to you.

@zhenlan zhenlan added this to the 2.1.0 milestone May 11, 2017
@zhenlan zhenlan modified the milestones: S118, 2.1.0 May 11, 2017
@epignosisx
Copy link
Author

Thanks folks for working on this. Linux support is not a priority for me.

@zhenlan zhenlan modified the milestones: 2.1.0, S118, S119 May 26, 2017
@zhenlan zhenlan modified the milestones: 2.1.0, S119 Jun 16, 2017
@mconnew
Copy link
Member

mconnew commented Aug 7, 2017

@epignosisx, were you able to verify this works for you on windows?

@Dudod
Copy link

Dudod commented Aug 14, 2017

Hi all. I have problem with configure proxy for http binding in .net core 2.0.
My code:

var binding = new BasicHttpBinding();
{
ProxyAddress = new Uri("http://192.168.0.1:8080"),
UseDefaultWebProxy = false
};
EndpointAddress endpoint = new EndpointAddress("http://someendpoint.com/SomeService");
_channelFactory = new ChannelFactory(binding, endpoint);
IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
TResult result;
try
{
result = await serviceCall((T)proxy).ConfigureAwait(false);
return result;
}
catch(Exception)
{
proxy.Abort();
return default(TResult);
}

After call service i have Exception: "When using a non-null Proxy, the WindowsProxyUsePolicy property must be set to WindowsProxyUsePolicy.UseCustomProxy." But i can`t set this property. In .net core 1.0 this code work correctly. How i can do it? Maybe i use it wrong?

@Dudod
Copy link

Dudod commented Aug 15, 2017

I have investigation about this problem. Important nootice - I talk about windows. I think problem in ServiceModelHttpMessageHandler used in WCF for Win. It wrapper for WinHttpHandler. Key difference between it and HttpClientHandler used in Unix build - it not set WindowsProxyUsePolicy.UseCustomProxy in case use proxy.

@zminic
Copy link

zminic commented Jan 18, 2018

Hi, I have a strange problem with BasicHttpBinding and proxy settings that is probably related to this issue.

The situation in our company is that we used to have corporate proxy and then at some point they removed it. We removed proxy from all windows settings and everything works as expected except WCF calls. WCF somehow still wants to use proxy even if there is no proxy configured in windows (also cleared automatic proxy detection from windows).

Opening urls in browser works, using HttpClient from aspnet core works, only WCF calls throw exception:

"WinHttpException: The server name or address could not be resolved", I have verified in wireshark that there is request for DNS resolve of old proxy, even if it is not configured anywhere.

Then I tried to set UseDefaultWebProxy on BasicHttpBinding to false and then I get following error:

"InvalidOperationException: When using a non-null Proxy, the WindowsProxyUsePolicy property must be set to WindowsProxyUsePolicy.UseCustomProxy."

I should also mention that when using VPN to access my workplace connections succeed.

Do you have a hint why is this happening and why only WCF calls require old proxy even if it is not configured anywhere?

@mconnew
Copy link
Member

mconnew commented Jan 19, 2018

@zminic, HttpClient in .Net Core doesn't use WinINet which picks up its proxy settings from the same place as you configure IE/Edge proxy settings. It uses WinHTTP instead which maintains its own proxy settings. From an administrative command prompt execute the command netsh winhttp reset proxy to reset WinHTTP to use direct connections instead. If you want to see what it's currently set as, execute the command netsh winhttp show proxy before you modify it. It looks like your IT department probably missed resetting this configuration when getting rid of the proxy.

@zminic
Copy link

zminic commented Jan 19, 2018

@mconnew Thanks a lot! It worked. Didn't know that .Net core uses different proxy settings.

@mconnew
Copy link
Member

mconnew commented Jan 27, 2018

The fix for broken proxy usage will be in the preview 1 release for .Net Core 2.1

@ctaggart
Copy link

ctaggart commented Jun 19, 2018

Based on this thread, to proxy to Fiddler on Windows 10, I used these commands as admin:

netsh winhttp show proxy
netsh winhttp set proxy 127.0.0.1:8888
netsh winhttp reset proxy

On Linux, is http_proxy environment variable supported? I guess I'll find out.

@mconnew
Copy link
Member

mconnew commented Jun 28, 2018

@ctaggart, in your use case WCF is configuring HttpClientHandler to use the default proxy. The behavior of what that means on Linux is a question for the dotnet/corefx repo as we don't do anything special there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants