defaultProxy.useDefaultCredentials in WCF client programmatically - c#

What code I should write to get same behavior as I set in config file
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>

This work for me:
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

Related

Web Config Error Causing 500 Internal Server Error

I have this inside my Web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="admin#xxxxxx.com">
<network defaultCredentials="false" host="mail.xxxxxx.com" password="xxxxxxx" port="25" userName="admin#xxxxxx.com" enableSsl="false" />
</smtp>
</mailSettings>
</system.net>
This causes any request to my website becomes
500 Internal Server Error
This makes the hosting server assumes that my Web.config is not valid. Before it was fine, my Web site can run with no problem. I cannot see any syntax error in it?
What is going on here?
Posting from comment thread as answer: You likely have a special character in your password that may require XML encoding.

C# ASP.NET MVC Access SMTP settings from config file

I seem to be stuck. I have an asp.net mvc 4 app and I want to send email from a service class. The service class is in a C# library project separate from the mvc project in the solution. I want to set the email configuration in a separate file that is called from a config file. Should the following go in the web.config in the mvc project or create an app.config file in the service project?
<system.net>
<mailSettings>
<smtp configSource="MailSettings.config" />
</mailSettings>
</system.net>
Then I have this in a separate file called MailSettings.config:
<smtp from="abc#email.com">
<network host="smtp.server.com" port="587" userName="abc#email.com" password="password" enableSsl="true" />
</smtp>
I have tried creating an app.config file with just the system.net stuff for the service project, but the mail settings are always null when I try:
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)ConfigurationManager.GetSection("system.net/mailSettings");
SmtpClient SmtpServer = new SmtpClient(settings.Smtp.Network.Host); //get null exception for settings.Smtp.Network.Host)
Also, I've tried including the mail settings in the app.config file to rule out having the MailSettings.config file being the issue and that still generates a null pointer.
I tried an example for accessing it from the web.config file like so:
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
In the service class though the WebConfigurationManager is out of context and so is the Request.ApplicationPath. So if I do have to get it from the web.config file, would I have to pass the http request object to the service class? That seems like a bad idea.
Just use:
SmtpClient SmtpServer = new SmtpClient();
This constructor will pick the configuration directly.
As the MSDN documentation for the parameterless constructor of SmtpClient says:
This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files. For more information, see <mailSettings> Element (Network Settings).

ASP.net SMTP Mail though Proxy

How to setup SMTPClient in ASP.net with C# to send email with provided proxy address ? or sending by detecting the system default proxy
I used the the following code in web.config but didnt work
<system.net>
<defaultProxy enabled="true">
<proxy bypassonlocal="False" proxyaddress="http://192.168.101.3:8080/" />
</defaultProxy>
</system.net>
You can't send SMTP email through an HTTP proxy server.
Speak to the person administering your Internet gateway/firewall to ask what host they want you to direct outbound email to.
Based on this article, you simply have to declare your proxy configuration into your Web.config and try to change the port:
If you are still having problems them try changing port number to 587
Edited my answer after your edit.

HTTP status 407: Proxy authentication required Error when calling web service

I have spent two hours on this error "HTTP status 407: Proxy authentication required Error " when calling a web service, my code is like following
WebProxy oWebProxy = new System.Net.WebProxy(ProxyServer, ProxyPort);
oWebProxy.Credentials = new NetworkCredential(ProxyUser,ProxyPassword,ProxyDomain);
oserv.Proxy = oWebProxy;
oserv.Credentials = new NetworkCredential(theusername, thepassword);
I have verified that the proxy address, user id password are all correct, and I could access the web service thru the IE in same pc, but when I run the code with VS, the error keeps popup.
I have tried the UserDefaultCredentials=true as well, but no luck.
Any idea?
Popping a<defaultProxy /> element into in app.config / web.config under <system.net> with useDefaultCredentials="true" may well do the job.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
Try to run the application in on same network where the webservice is installed

getting "remote server cannot be resolved" on localhost does not get fixed by defaultProxy?

here is the sitution, i am testing on my localhost from my machine at home (no proxy server and windows default firewall) and retrieving api.flickr.com xml file, when I come to work (that uses an ISA server to connect) I get "remote server could not be resolved" so I added these lines to the web.config
<system.net>
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://localhost"
bypassonlocal="True"
/>
<bypasslist>
<add address="[a-z]+\.flickr\.com\.+" />
</bypasslist>
</defaultProxy>
</system.net>
that returns:System.Net.WebException: The remote server returned an error: (404) Not Found.
what went wrong? thanks
There are two possible scenarios here:
1: If you are building a client app (e.g. Console or WinForms) and want to access http://localhost using WebClient or HttpWebRequest without any intervening proxies, then bypassonlocal="True" should accomplish this. In other words, your app.config should look like this:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
2: if, however, you're trying to get your ASP.NET app (running on http://localhost) to be able to correctly resolve URIs either with a proxy or without one, then you'll need to set up proxy info correctly in your web.config (or in machine.config so you won't have to change your app's web.config), so ASP.NET will know that you are running a proxy or not running one. Like this:
Home:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
Work:
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://yourproxyserver:8080"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
It's also possible to use proxy auto-detection, to pick up settings from the registry, etc. but I've always shied away from those approaches for servers apps... too fragile.
BTW, if you find that things are configured correctly, and you still get the error, the first thing I'd recommend is to code up a quick test which manually sets the proxy before your WebClient/HttpWebRequest call, instead of relying on configuration to do it. Like this:
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebClient wc = new WebClient();
wc.Proxy = proxyObject;
string s = wc.DownloadString ("http://www.google.com");
If the requests don't go through your work proxy correctly even when you're using code, even if the proxy is correctly configured in your code, then the proxy itself may be the problem.
In WebClient to download data from local no issues but downloading from internet is problem so configure the following
In your Web.config add below lines and replace your Intenet proxy Address and port
<system.net>
<defaultProxy useDefaultCredentials="true" enabled="true">
<proxy usesystemdefault="False" proxyaddress="http://your proxy address:port" bypassonlocal="True" />
</defaultProxy>
<settings>
<servicePointManager expect100Continue="false" />
</settings> </system.net>
now your program logic work for downloading content from internet and public URLS.

Categories