C# force windows service to use proxy - c#

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

Related

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.

Certificate validation doesn't use proxy settings for chaintrust

I've been struggling to get a WCF client to work through a web proxy. If I manually specify the proxy as below, I can get the http request to work.
WebProxy proxy = new WebProxy("http://x.x.x.x:3128", false);
proxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = proxy;
However I have the client service proxy set to use ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;
Inspecting the packets that get sent out, the client successfully connects to the server, but the then tries to validate the certificate that the service provides. This validation attempt fails because the requests for the chain do not have Proxy-Authorization headers (they fail with 407 errors). How can I get these requests to properly use the DefaultWebProxy that's specified?
If I set the validation mode to None, then it all works of course, but I really don't want to have to do that.
this setting only affects message level certificates. for transport level try something like this http://webservices20.blogspot.co.il/2008/12/wcf-gotcha-disabling-ssl-validation.html

Passing credentials from ASP.NET C# to another web service

I have a one page ASP.NET 4.0 C# script running. In debug mode my script works just fine but when I publish it, it seems like it is not sending the credentials when making the WebRequest.
The following is the code I am using, I have tried a bunch of things but I keep getting a 401 Unauthorized when I am using my published version of the script. BTW I am using IIS 7
WebRequest fwRequest = HttpWebRequest.Create(fwURL);
fwRequest.UseDefaultCredentials = true;
//fwRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)fwRequest.GetResponse();
While debugging, your project use your credentials, but when you publish it it will use the credentials of ASP.NET user, or the credentials set in IIS configuration.
So no, you can not pass credentials of your client to another web service/appliaction unless you convince him/her to pass username-password to your application (Except that both web apps are on the same machine).

WebProxy and Authentication on Vista

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)

Categories