Reputation: 121
How to use the saved proxy settings and credentials as default for HttpWebRequests? The proxy settings are accessible and used but not the credentials:
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy = proxy;
Are there any permissions on using the credentials?
It works with passing the credentials via NetworkCredential instance:
proxy.Credentials = new NetworkCredential("username", "password");
But I would like to use the saved ones from the operating system/IE.
Edit:
I am using a third party lib creating and calling the HttpWebRequests which should be passed through the proxy. Could that be part of the problem?
Using the App.Config file with this content doesn't work either
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>
</system.net>
</configuration>
Upvotes: 7
Views: 8235
Reputation: 1
This worked for me. Of course you will have to add the URL. The code snip-it was pulled from an app I made that sends XML Soap Requests and responses to one companies web-service that needed to be usable while on or off another companies Cisco Any-Connect network.
public HttpWebRequest CreateWebRequest()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
IWebProxy proxy = request.Proxy;
if (proxy != null)
{
string proxyuri = proxy.GetProxy(request.RequestUri).ToString();
request.UseDefaultCredentials = true;
request.Proxy = new WebProxy(proxyuri, false);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
return request;
}
App.config file:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>
</system.net>
Upvotes: 0
Reputation: 1616
I was having this exact same issue maybe two weeks ago!! HTTP 407 errors. I tried both suggestions, set network credentials and add the default proxy line in the app.config but neither worked. Strange too because I didn't have this issue on a vb application I wrote last year that did the exact same thing, go out to a web site and download the source as a string. When I did it then, I ran into this issue but adding the defaultProxy line in App.config worked! Weird.
This is how I was able to get around the issue.
I don't create a proxy, I assign the default one (pulled from app.config) to the webRequest's proxy attribute. I then ensure that "UseDefaultCredentials" is set to "true".
HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest);
request.Proxy = WebRequest.DefaultWebProxy;
request.UseDefaultCredentials = true;
I added this to the app.config (same as the above post).
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
Here's the kicker (and I wish I could explain why this solved my issue). Right after I create the request and assign the proxy with the correct credentials... I had to create a cookie container and assign it to my web request. Seriously, I don't know why this solved this issue because I didn't have to do it before.
CookieContainer cc = new CookieContainer();
request.CookieContainer = cc;
I hope this helps you.
Here's the complete code (minus the app.config lines):
HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest);
request.Proxy = WebRequest.DefaultWebProxy;
request.UseDefaultCredentials = true;
CookieContainer cc = new CookieContainer();
request.CookieContainer = cc;
HttpWebResponse response = (request.GetResponse() as HttpWebResponse);
Upvotes: 7
Reputation: 3352
Check your app.config
(or web.config
) file, if it exists. If these proxy settings are going to be application-wide, then make sure you have the following in that file (create it if necessary):
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>
</system.net>
</configuration>
You won't need to have anything else in your code; all WebRequest
instances will simply use the proxy.
If your proxy settings are not going to be application-wide, then make sure those settings are not in the .config
file above, and be sure to set them in code as required.
Upvotes: 1
Reputation: 156958
As klugerama already suggested you should use CredentialCache.DefaultNetworkCredentials
.
This contains the network credentials even when they seem empty.
I had like to paste another answer from this post on SO with an excellent explanation:
The NetworkCredential returned from CredentialCache.DefaultCredential is just a placeholder. ... Internal API check for this type to see if integrated authentication should be used or not.
So the things you should check is:
Upvotes: 1