WebRequest Proxy Authentication
I have a client application written in C# and I use a Web Reference to a SOAP webservice. Now this works fine on my development machine – and indeed it continued to work fine, up until I tested it from behind a proxy.
Many hours of searching MSDN and Google later I was a little the wiser (not much) about how to deal with proxy’s and authentication with .NET (I’m using v2 of the framework)
The first thing to check is whether .NET will auto detect the fact there is a proxy (from IE) or whether we have to set this manually. This depends on the <defaultProxy> element in the .config file. Check out this page for info: http://msdn2.microsoft.com/en-us/library/sa91de1e(VS.80).aspx Particularly the following note:
“The proxy element defines a proxy server for an application. If this element is missing from the configuration file, then the .NET Framework will use the proxy settings in Internet Explorer.”
So – what we could assume from this is that if the setting is not there – all should work fine and dandy… well this would be true up to a point – and that point is authentication.
Authentication
Does the Proxy server authenticate using the default credentials of the user logged into windows? If so then we either want:
a) useDefaultCredentials=”true” in the default proxy config element, also see: http://geekswithblogs.net/mnf/archive/2006/03/08/71663.aspx
b) or we want to “WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;”
Credentials arnt stored in the Default Credentials? This is the situation you are in if have a user/pass box popup when you first navigate to a page in IE.
NetworkCredential nc = new NetworkCredential("ProxyUser","ProxyPassword", "ProxyDomain");
WebRequest.DefaultWebProxy.Credentials = nc;
Other interesting reading about a specific problem that you might be having:


September 23rd, 2009 at 9:23 am
Hello,
I am using proxy to send sms from client site.
Below is my code which runs nicely on localhost but not working on Live Server, it gives me error like below:
Request for the permission of type ‘System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.
Dim result As String = “”
Dim request As WebRequest = Nothing
Dim response As HttpWebResponse = Nothing
Dim sendToPhoneNumber As String = CStr(O)
Dim userid As String = “xyzabcd”
Dim passwd As String = “abcsjklp”
Dim msgs As String = msgBox
Dim url As String
url = “http://x.y.com/I/rest?method=sendMessage&send_to=” & sendToPhoneNumber & “&msg=” & msgs & “&userid=” & userid & “&password=” & passwd & “&v=1.1″
request = WebRequest.Create(url)
Dim proxy As New webProxy(”http://x.y.com/I/”, True)
proxy.UseDefaultCredentials = True
proxy.Credentials = New NetworkCredential(”xxxxxx”, “yyyyy”, “http://11.11.11.11:80/”)
WebRequest.DefaultWebProxy = proxy
request.Proxy = proxy
response = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim ec As Encoding = System.Text.Encoding.GetEncoding(”utf-8″)
Dim reader As StreamReader = New System.IO.StreamReader(stream, ec)
result = reader.ReadToEnd()
Console.WriteLine(result)
reader.Close()
stream.Close()
September 28th, 2009 at 1:18 am
Perhaps this might help you?
http://consultingblogs.emc.com/kenibarwick/archive/2004/11/05/Request-for-the-permission-of-type-System.Net.WebPermission_2100210021002100_.aspx