Is it possible to specify proxy credentials in your web.config?
Yes, it is possible to specify your own credentials without modifying the current code. It requires a small piece of code from your part though.
Create an assembly called SomeAssembly.dll with this class :
namespace SomeNameSpace
{
public class MyProxy : IWebProxy
{
public ICredentials Credentials
{
get { return new NetworkCredential("user", "password"); }
//or get { return new NetworkCredential("user", "password","domain"); }
set { }
}
public Uri GetProxy(Uri destination)
{
return new Uri("http://my.proxy:8080");
}
public bool IsBypassed(Uri host)
{
return false;
}
}
}
Add this to your config file :
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type = "SomeNameSpace.MyProxy, SomeAssembly" />
</defaultProxy>
This "injects" a new proxy in the list, and because there are no default credentials, the WebRequest class will call your code first and request your own credentials. You will need to place the assemble SomeAssembly in the bin directory of your CMS application.
This is a somehow static code, and to get all strings like the user, password and URL, you might either need to implement your own ConfigurationSection, or add some information in the AppSettings, which is far more easier.
How to pass credentials in defaultProxy config setting?
I'm not aware of a way to do this in defaultProxy section of web.config, but you can definitely do it from code. Try this:
// Get proxy server info from AppSettings section of Web.Config
var proxyServerAddress = ConfigurationManager.AppSettings[ "proxyServerAddress" ];
var proxyServerPort = ConfigurationManager.AppSettings[ "proxyServerPort" ];
// Get proxy with default credentials
WebProxy proxy =new WebProxy(proxyServerAddress, proxyServerPort);
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials();
Web.Config (configuration section):
<appSettings>
<add key="proxyServerAddress" value="proxy.myhost.com" />
<add key="proxyServerPort" value="8080" />
</appSettings>
And then assign proxy
to the webClient you are using in your webPart.
EDIT:
If I had done more homework, I would have realized your problem could have been fixed with one attribute: useDefaultCredentials="true"
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True" />
</defaultProxy>
</system.net>
How should I set the default proxy to use default credentials?
From .NET 2.0 you shouldn't need to do this. If you do not explicitly set the Proxy property on a web request it uses the value of the static WebRequest.DefaultWebProxy. If you wanted to change the proxy being used by all subsequent WebRequests, you can set this static DefaultWebProxy property.
The default behaviour of WebRequest.DefaultWebProxy is to use the same underlying settings as used by Internet Explorer.
If you wanted to use different proxy settings to the current user then you would need to code
WebRequest webRequest = WebRequest.Create("http://stackoverflow.com/");
webRequest.Proxy = new WebProxy("http://proxyserver:80/",true);
or
WebRequest.DefaultWebProxy = new WebProxy("http://proxyserver:80/",true);
You should also remember the object model for proxies includes the concept that the proxy can be different depending on the destination hostname. This can make things a bit confusing when debugging and checking the property of webRequest.Proxy. Call
webRequest.Proxy.GetProxy(new Uri("http://google.com.au"))
to see the actual details of the proxy server that would be used.
There seems to be some debate about whether you can set webRequest.Proxy
or WebRequest.DefaultWebProxy = null
to prevent the use of any proxy. This seems to work OK for me but you could set it to new DefaultProxy()
with no parameters to get the required behaviour. Another thing to check is that if a proxy element exists in your applications config file, the .NET Framework will NOT use the proxy settings in Internet Explorer.
The MSDN Magazine article Take the Burden Off Users with Automatic Configuration in .NET gives further details of what is happening under the hood.
Default proxy in App.Config cause .NET application to crash with no error
This is what I ended up doing in the end (basically what NightOwl888 suggested) with GetSystemWebProxy added
var proxy = WebRequest.GetSystemWebProxy();
_webRequestHandler = new WebRequestHandler { ClientCertificateOptions = ClientCertificateOption.Automatic };
_webRequestHandler.Proxy = proxy;
_client = new HttpClient(_webRequestHandler);
_client.BaseAddress = new Uri(connectionUrl);
_client.Timeout = new TimeSpan(0,0,0,timeoutSeconds);
How to set proxy settings for IIS processes?
All I needed was to set system.net
in web.config
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://10.0.2.231:42"
bypassonlocal="true"
/>
</defaultProxy>
</system.net>
See: Element (Network Settings).
System.Data.DataSet.ReadXml - Proxy Authentication Required
it means, that your credentials for the proxy server are incorrect, best solution to try and approach this problem would be:
First, add this line to your Web.Config
:
<system.net>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>
Second, is through code:
service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
third, is to set the credentials in two locations through code:
HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");
it's whatever suits you best here.
Related Topics
How to Get the File Size in C#
Make Listview.Scrollintoview Scroll the Item into the Center of the Listview (C#)
How to Get and Set the Window Position of Another Application in C#
What Are the Downsides to Turning Off Proxycreationenabled for Ctp5 of Ef Code First
Excel to Datatable Using Epplus - Excel Locked for Editing
What Advantages of Extension Methods Have You Found
On Postback, How to Check Which Control Cause Postback in Page_Init Event
How to Sort an Observable Collection
C# Sending Mails with Images Inline Using Smtpclient
Is It Necessary to Manually Close and Dispose of SQLdatareader
C# Optional Parameters on Overridden Methods
Compare Version Numbers Without Using Split Function
Is Async Await Keyword Equivalent to a Continuewith Lambda
In C# Check That Filename Is *Possibly* Valid (Not That It Exists)
ASP.NET MVC: How to Redirect a Non Www to Www and Vice Versa