WebProxy and Authentication on Vista - c#

I am talking to a webservice via a webrequest, I am behind a proxy that requires authentication.
What I would like to do is piggyback off the IE / Control Panel settings but I am having some difficulty...
if I do this, all is fine...
WebProxy proxy = new WebProxy(#"http://my.secret.address:8080");
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy = proxy;
What I really want to do is simply this...
WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy(); // Subsequent webrequest call Fails with a "Unable to connect to remote server" error message.
I really do not want to have to specify the proxy address, as it is not the same for all users. In fact some won't even be behind a proxy. I just want to use the IE /Control Panel settings. Oh I am using Vista in case that makes a difference, and also the proxy settings in th econtrol panel / IE are using an auto config file (proxy.pac file)
Edit: So succinctly. How do I use the IE / Control Panel proxy settings. Including when using an Auto configuration file ?
Further Edit:
Ok, I think I have narrowed down the problem to the Auto Config thing. If I have the proxy address explicitly set in the dialog I can use the .GetSystemWebProxy() settings...but (like in my case) if I am using an Auto Config pac file, I have this issue.
alt text http://img40.imageshack.us/img40/5635/57955210.jpg

In .NET 1.0, you could use:
WebRequest.DefaultWebProxy = WebProxy.GetDefaultProxy();
In 2.0, DefaultWebProxy is supposed to contain the IE proxy settings by default, so this method is obsolete.
http://www.west-wind.com/WebLog/posts/2542.aspx has more information.
UPDATE: Apperently the .NET 2.0 method is now;
WebRequest.DefaultProxy = WebRequest.GetSystemWebProxy();
http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx

sigh, well after more investigation I fixed this problem just to get a different one....
The fix is to create the WebProxy with the .pac Uri
WebProxy proxy = new WebProxy(#"http://blahblah/proxy.pac);
Easy peasy...
So now I am getting through the proxy, but the Proxy server is messing with my request and the web service is barffing. (Note it doesn't do it if I am specific about the proxy address....sigh)

Related

C# force windows service to use proxy

Using Proxy Switcher with a private proxy (login & password authentication). In debug mode my win service works fine: it runs identically to console app under current logged on user with proxy set for browser. This code do the job just fine (may be, even a bit excessive):
// use default proxy settings (like those in browser)
HttpWebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
// create web request object for URL
HttpWebRequest webreq = (HttpWebRequest)HttpWebRequest.Create(URL);
// set default proxy credentials for object
webreq.Credentials = CredentialCache.DefaultNetworkCredentials;
But... it stops working when running compiled release of win service under Local System, i.e. it queries URL via direct internet connection without proxy. How to make win service to use proxy, which is set for IE by Proxy Switcher or even hardcoded in c# (that will do too)?
Made hardcoded proxy, it works without Proxy Switcher:
// use hardcoded proxy (works for win service under local system account)
WebProxy proxy = new WebProxy("000.000.000.00", 50100); // ip & port
proxy.Credentials = new NetworkCredential("login", "password");
webreq.Proxy = proxy;
Still interested in more flexible variant with proxy inherited by win service automatically - that initially set by Proxy Switcher and used in web browser. Pls. advise

mono http requests behind proxy

I've written a C# app that performs web requests via the System.Net.Http.HttpClient (e.g. client.GetAsync(uri);). Compiling and executing it with the .Net runtime, all these calls are successful. However, compiling and running with Mono, they fail with exceptions ("IOException Authentication or decryption failed").
However, switching to a network that doesn't go through the proxy resolves the issue. So in conclusion, it's not a certificate or whatever issue, but just the issue of the proxy.
Same applies for the tlstest tool: Fails miserably with the proxy, works fine without it.
How do I configure mono to use the proxy settings / use the system proxy settings?
Can you try setting the proxy in HttpClient? You can set proxy credentials if necessary.
WebProxy proxy = new WebProxy("proxyaddress", port);
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = proxy
};
HttpClient client = new HttpClient(handler);
Try to use modernhttpclient component. This library brings the latest platform-specific networking libraries to Xamarin applications via a custom HttpClient handler which can handle for you this kind of issues.

How to use proxy like browser OR CredentialCache.DefaultCredentials different between XP and 7

I am able to fix a problem with a client where they cannot authenticate through a proxy doing the following:
var proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
service.Proxy = proxy;
This works fine for Windows XP, however on Windows 7 I get a 407 (proxy not authenticated exception). Does anybody know what the difference is, and more importantly, what I need to do to get this to work on both OS?
UPDATE
I am having the users check the following:
In the registry editor, can you go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon and let me know what the value is for CachedLogonsCount. f
In the Start box, type in Group Policy and an option to Edit Group Policy should pop up, click on it. Then go to Computer Configuration\Administrative Templates\System\User Profiles\Delete cached copies of roaming profiles and let me know if it is configured, and if so, to what is it set?
UPDATE FOR BOUNTY
So, I added the bounty. I can take a solution from here, or just an alternate means to getting through a proxy on Windows 7...
Another Update
I am not sure if this is useful or not, but we are also doing the following:
service.PreAuthenticate = true;
service.Url = "myurl";
service.Credentials = new NetworkCredential(txt_UserName.Text, txt_Password.Text);
My temporary solution
This is not really a solution, but works for now. I am using the app.config and setting the proxy to be default, with a ByPassList so that the proxy is not even used. This is only doable since the proxy does not have a strong firewall currently. For other clients, I need to get the above to work
This piece of code works for me on XP, Win7 and 2008
var webProxy = new WebProxy(WebRequest.DefaultWebProxy.GetProxy(new Uri({TheURLoftheService})));
webProxy.Credentials = CredentialCache.DefaultCredentials;
webProxy.UseDefaultCredentials = true;
service.Proxy = webProxy;
actually looks like they "fixed" it in Win7 :) Can you confirm that both client and server are specifying http 1.1
Now let's discuss as to why the browser works in this scenario. IE
uses WinINet under the hood rather than WinHTTP. If we look at the
network traces we see that IE sends HTTP/1.1, but the proxy replies
with HTTP/1.0. IE still accepts this behavior, because in the internet
scenario there are countless number of clients and servers which still
use HTTP/1.0.
WinHTTP strictly requires HTTP/1.1 compliance for keeping the
connection alive and HTTP Keep-Alives are not supported in HTTP/1.0
protocol. HTTP Keep-Alive feature was introduced in the HTTP/1.1
protocol as per RFC 2616. The server or the proxy which expects the
keep-alive should also implement the protocol correctly. WinHTTP on
Windows 7, Windows 2008 R2 are strict in terms of security wrto
protocol compliance. The ideal solution is to change the server/proxy
to use the right protocol and be RFC compliant.
http://blogs.msdn.com/b/httpcontext/archive/2012/02/21/changes-in-winhttp-on-windows-7-and-onwards-wrto-http-1-0.aspx
Will this work?
I am using this to set proxy, so far we did not encounter an error on all windows platform
Uri address = new Uri("http://your-webservice-address");
//Get User current network credential
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(address, "Basic");
//Get HttpWebRequest
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
//Network Credential should be included on the request to avoid network issues when requesting to the web servic
request.Proxy = WebRequest.DefaultWebProxy;
request.Credentials = new NetworkCredential(credential.UserName, credential.Password, credential.Domain);
It's hard to say based on the code you've given. I'd suspect that it's either your IE settings or your proxy variables.
Check http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/61b71194-1758-4c7b-89fe-91be7363db13 it may help.

How to Specify a Web Proxy for a Service Reference?

I found a simple web service online at http://chennaiemergency.co.in/sree/s2.php?wsdl which i am able to invoke through SOAP UI using 2 float values (1,1) and easily get the response within 1-2 seconds.
Now, in a new visual studio 2010 console application project I do "add service reference" and provide the WSDL. Then in the resulting client i do this :
ServiceReference1.ChnEmergencyPortTypeClient client = new ChnEmergencyPortTypeClient();
string hospital = client.hospital(1, 1);
I get timeout exception after 1 minute.
I have disabled firewall for sure.
I am using Windows7x64
I am using internet via proxy server.
I tried same thing via adding web reference, but i got same timeout error.
Now in the web reference implementattio I have done these few modifications :
WebProxy webProxy = new WebProxy("<my proxy server name>", <port>);
ChnEmergency client = new ChnEmergency();
client.Timeout = 200000;
client.Proxy = webProxy;
string hospital = client.hospital(1, 1);
But i still get timeout. Any suggestions where i am missing ?
I did a quick test with a direct (via phone) internel connection which does not involve proxy servers. And i was able to access successfully. This indicates that there is fault in the way i am providing the webproxy. The IE internet setting suggests that my proxy settings are :
Address : a.b.c.com
Port : 80
So i am bulding a webproxy like this
WebProxy webProxy = new WebProxy("a.b.c.com", 80);
Now i dont know if there is some "secure http" concept involved somewhere, nor do i know how to figure out. But a quick try on browser with http://a.b.c.com and https://a.b.c.com yielded different results. In the "http" case i got that invalid url. In "https" case error is "Google Chrome's connection attempt to a.b.c.com was rejected. The website may be down, or your network may not be properly configured"
If i use webproxy with https, it says "service point manager is not configure for https"
I used fiddler to see the activities and I see that the request does show up in fiddler. But no response comes. Does this necessarily mean that the request is going through ? Or could the request get blocked at a lower level (ie. after it goes through fiddler).
I tried quickly making a small web project and it seems to work fine, but the way you have added the webservice it returns xml like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:hospitalResponse xmlns:ns1="urn:ChnEmergency">
<return xsi:type="xsd:string">12.944672~80.134578~Chrompet GH~Government~GST Road, Chrompet~Chrompet~600036</return>
</ns1:hospitalResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If your Proxy needs a password, you could try:
var proxy = new WebProxy("proxy.foo.com", 8080);
proxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = proxy;
Well it turns out that the proxy i was using, proxyA, was inturn redirecting through another proxy, proxyB. Using the proxyB directly resolved the issue. I was informed about this by someone else, didnt figure out myself. I used the proxy object like WebProxy webProxy = new WebProxy("proxyB.com", 80). So issue is not solved, but resolved for now.

why won't WebProxy BypassProxyOnLocal work for me?

I'm trying to get HTTP calls I'm making from C# .NET to a local address (localhost:3000) to use the proxy I set (so I can go through fiddler). Using the below WebProxy approach works if I point the target URL to a non-local address, however I need to point it to a local web-server I have (at localhost:3000), and when I do this the request is not going through the proxy.
I have inlcuded the "proxyObject.BypassProxyOnLocal = false". This should make it work no? Any suggestions re how to force the request to go through the WebProxy for http calls targetting a local address?
WebProxy proxyObject = new WebProxy("http://localhost:8888/", false);
proxyObject.Credentials = new NetworkCredential();
proxyObject.BypassProxyOnLocal = false;
WebRequest.DefaultWebProxy = proxyObject;
var request = (HttpWebRequest)WebRequest.Create(targetUri);
// I also included this line as a double check
request.Proxy = proxyObject;
Subsequent calls do not go through the proxy however, such as when I do:
var res = (HttpWebResponse)req.GetResponse();
thanks
I get around this simply by appending a "dot" to localhost, so instead of accessing "localhost", I try to access "localhost." (notice the dot at the end of the hostname)
Credit where credit is due:
I got this unusual tip from this thread http://www.west-wind.com/weblog/posts/2009/Jan/14/Monitoring-HTTP-Output-with-Fiddler-in-NET-HTTP-Clients-and-WCF-Proxies#596591
Works fine!
See explanation on https://docs.telerik.com/fiddler/observe-traffic/troubleshooting/notraffictolocalhost
Internet Explorer and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic.
The simplest workaround is to use your machine name as the hostname instead of Localhost or 127.0.0.1. So, for instance, rather than hitting http://localhost:8081/mytestpage.aspx, instead visit http://machinename:8081/mytestpage.aspx.

Categories