Programmatically Set Proxy Address, Port, User, Password throught Windows Registry - c#

I'm writing a small C# application that will use Internet Explorer to interact with a couple a websites, with help from WatiN.
However, it will also require from time to time to use a proxy.
I've came across Programmatically Set Browser Proxy Settings in C#, but this only enables me to enter a proxy address, and I also need to enter a Proxy username and password. How can I do that?
Note:
It doesn't matter if a solution changes the entire system Internet settings. However, I would prefer to change only IE proxy settings (for any connection).
The solution has to work with IE8 and Windows XP SP3 or higher.
I'd like to have the possibility to read the Proxy settings first, so that later I can undo my changes.
EDIT
With the help of the Windows Registry accessible through Microsoft.Win32.RegistryKey, i was able to apply a proxy something like this:
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
But how can i specify a username and a password to login at the proxy server?
I also noticed that IE doesn't refresh the Proxy details for its connections once the registry was changed how can i order IE to refresh its connection settings from the registry?
Thanks

For IE, you can use the same place in the registry. Just set ProxyServer="user:password#127.0.0.1:8080" however firefox completely rejects this, and does not attempt to connect.

Here's how I've done it when accessing a web service through a proxy:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
WebProxy proxy = new WebProxy(proxyUrl, port);
NetworkCredential nwCred = new NetworkCredential(proxyUser, proxyPassword);
CredentialCache credCache = new CredentialCache();
credCache.Add(proxy.Address, "Basic", nwCred);
credCache.Add(proxy.Address, "Digest", nwCred);
credCache.Add(proxy.Address, "Negotiate", nwCred);
credCache.Add(proxy.Address, "NTLM", nwCred);
proxy.Credentials = credCache;
proxy.BypassProxyOnLocal = false;
request.Proxy = proxy;

Related

Errors appear after connecting to Exchange Web Services (EWS) in C#

First of all, sorry for my poor english...
Let's see if I can summarize the problem, it is quite strange:
Before we start... I have 2 users inside my company's domain.
User1 --> Has a proxy configured
User2 --> NO proxy configured
You are only able to navigate if you have the company's proxy configured, otherwise it won't load any webpage
1 - I log on into my computer using User1 domain credentials
2 - First, I open Internet Explorer, and I can navegate to any webpage, everything works fine. (I keep the window opened)
3 - I have a C# aplication running on my computer, which connects to EWS and logs into the mailbox of User2. The code looks like this:
ExchangeService serviceInstance = null;
serviceInstance = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
serviceInstance.DateTimePrecision = DateTimePrecision.Milliseconds;
serviceInstance.Credentials = new WebCredentials(user, pass, domain);
serviceInstance.AutodiscoverUrl(mailbox);
After I connect to the mailbox, I loop the messages for different purposes, everything works fine, and I close the aplication.
4 - I open the same IE window from step 1 (I didn't close it) and I try to load any other page, for example, https://google.com the page doesn't load and it shows me a certificate error:
Certificate error
If I click on 'Continue to this website', a firewall error appears.
Firewall error
5 - If I close the IE window and open a new window, something, I don't know what, seems to restart, and I can navigate again to any webpage.
I have explained this problem with the Internet Explorer case, but that's just to make it more visual. I have another aplications on this computer which need to make http calls, connect to webservices... And they sometimes fail because of this.
Does anybody know what is happening? How can I solve this?
Any help is appreciated!
You could try defining the proxy in your code eg
// set up the proxy
WebProxy proxy = new WebProxy("valid-proxy", 8080);
proxy.Credentials = new
NetworkCredential("valid-user","valid-password","valid-domain");
service.WebProxy = proxy;

DotNet HttpClient.DefaultProxy Property not reading system settings on Win10

The docs indicate, that the default Proxy-Setup for the HttpClient will be determined through the presence of environmental variables and/or the system wide proxy settings.
I set the proxy-auth credentials like this:
setx http_proxy http://user:password#proxyIP:proxyPort/, and to be sure rebootet the system.
Since the Windows proxy-settings dialog does not allow to set username and password, i included there the proxy address and port.
Now i try to read them back though the CredentialCache with my C# app.
However the CredentialCache.DefaultCredentials are still empty and the proxy blocks and responds with 407 exceptions. Which is correct, since i cannot pass the credentials.
How do i have to setup the username and password to be able to read them from the CredentialCache?
I would like to be able to go this way, since very old code, long time ago shipped to customers, using a WebRequest object together with this settings is able to go through.:
UseDefaultCredentials = true;
Proxy = WebRequest.GetSystemWebProxy();
Proxy.Credentials = CredentialCache.DefaultCredentials;
Thats why i would like to also be able to write somethin to Windows that in turn gets read out of the Cache.

IBMMQDotnetClient using windows credentials instead of application specified credentials

In using IBMMQDotnetClient v9.2.0.1 in .NET Core, I attempt to connect to a client using
private MQQueueManager Connect()
{
System.Collection.Hashtable properties = new System.Collections.Hashtable();
properties.Add(MQC.USER_ID_PROPERTY, "username");
properties.Add(MQC.PASSWORD_PROPERTY, "password");
// more settings below
return new MQQueueManager(QueueManagerName, properties);
}
The issue is this still attempts to pass my personal windows credentials even after having set the userid and password when configuring the queue manager properties. How do I ensure that the credentials passed by the application replace my personal windows creds when running the application.
Edit:
For more context, the output options are configured as:
public static readonly int OUTPUT_OPTIONS = MQC.MQOO_OUTPUT;
I am not sure if there can be other options that can ensure the passed creds are used as opposed to the windows creds.
For anyone in the future that runs into a similar situation I wanted to give an update on my resolution. I ended up running the IIS server with the credentials that were authenticated with MQ as opposed to a Network Service and this fixed the issue. This allowed me to use the queue manager that had already been set up, without changing its settings in any way.

C# FTP behind Proxy with FtpWebRequest [duplicate]

As I understand it, the FtpWebRequest.Proxy property denotes an HTTP proxy. I have to issue FTP requests to an external server via an FTP proxy.
The only way I've got this to work so far is by creating a script which uses the Windows FTP command and downloading that way.
Is it possible to use the FtpWebRequest to download files via an FTP proxy?
Here's code I've used before, I should caveat I've only tested this against a Checkpoint firewall so the format USER and PASS commands maybe different for your FTP Proxy. Your system administrator will know the correct format.
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://FTP PROXY HOST/actual/path/to/file/on/remote/ftp/server"));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential
("REMOTE FTP USER#FTP PROXY USER#REMOTE FTP HOST"
, "REMOTE FTP PASSWORD#FTP PROXY PASSWORD");
If the FTP proxy allows specifying all information about the target via USER and PASS commands, you can use the Credentials property.
Typically, you specify the username in a form user#proxyuser#host and password in a form password#proxypassword:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user#proxyuser#host", "password#proxypassword");
If the proxy does not require authentication, use a form user#host and password:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user#host", "password");
But your proxy server may also require a different syntax, like:
separate USER commands for proxy user and target host user
OPEN command
SITE command
In these cases, you cannot use the FtpWebRequest. You must use a 3rd party FTP client library instead.
For example with WinSCP .NET assembly, you can use:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "host",
UserName = "user",
Password = "password",
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
sessionOptions.AddRawSettings("FtpProxyLogonType", "2");
sessionOptions.AddRawSettings("ProxyUsername", "proxyuser");
sessionOptions.AddRawSettings("ProxyPassword", "proxypassword");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Your code
}
For the options for the SessionOptions.AddRawSettings, see raw settings.
Easier is to configure the proxy settings in WinSCP GUI and have it generate C# FTP code template for you.
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
(I'm the author of WinSCP)
If you have the budget for it - Dart do some great classes for this:
http://www.dart.com/
or specifically
http://www.dart.com/ptftpnet.aspx
I know this is way past the due date, but yes, I'm able to get FtpWebRequest to work with an ftp proxy by using the old tricks of setting the URL to
"ftp://your.proxy.server/theFileToDownload")"
Then set your network credential to
username="ftpUserName#ftp.realserver.com"
and
password="password".
I remembered doing this back in the bad old WININET days, and sometimes the old tricks are still the best tricks.
YMMV of course.
They claim it is possible to list, listdetails, and download through a proxy. However I could not get this to work with an isa firewall. So I disabled the default proxy in my app.config and added an application rule for ForeFront/ISA client. To do this I created a file c:\programdata\microsoft\firewall client 2004\application.ini with the contents:
[applicationName]
DisableEx=0
Disable=0
NameResolution=R
where applicationName is the running exe minus the .exe extension.

Is it possible to use C# FtpWebRequest through a FTP proxy?

As I understand it, the FtpWebRequest.Proxy property denotes an HTTP proxy. I have to issue FTP requests to an external server via an FTP proxy.
The only way I've got this to work so far is by creating a script which uses the Windows FTP command and downloading that way.
Is it possible to use the FtpWebRequest to download files via an FTP proxy?
Here's code I've used before, I should caveat I've only tested this against a Checkpoint firewall so the format USER and PASS commands maybe different for your FTP Proxy. Your system administrator will know the correct format.
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://FTP PROXY HOST/actual/path/to/file/on/remote/ftp/server"));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential
("REMOTE FTP USER#FTP PROXY USER#REMOTE FTP HOST"
, "REMOTE FTP PASSWORD#FTP PROXY PASSWORD");
If the FTP proxy allows specifying all information about the target via USER and PASS commands, you can use the Credentials property.
Typically, you specify the username in a form user#proxyuser#host and password in a form password#proxypassword:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user#proxyuser#host", "password#proxypassword");
If the proxy does not require authentication, use a form user#host and password:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user#host", "password");
But your proxy server may also require a different syntax, like:
separate USER commands for proxy user and target host user
OPEN command
SITE command
In these cases, you cannot use the FtpWebRequest. You must use a 3rd party FTP client library instead.
For example with WinSCP .NET assembly, you can use:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "host",
UserName = "user",
Password = "password",
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
sessionOptions.AddRawSettings("FtpProxyLogonType", "2");
sessionOptions.AddRawSettings("ProxyUsername", "proxyuser");
sessionOptions.AddRawSettings("ProxyPassword", "proxypassword");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Your code
}
For the options for the SessionOptions.AddRawSettings, see raw settings.
Easier is to configure the proxy settings in WinSCP GUI and have it generate C# FTP code template for you.
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
(I'm the author of WinSCP)
If you have the budget for it - Dart do some great classes for this:
http://www.dart.com/
or specifically
http://www.dart.com/ptftpnet.aspx
I know this is way past the due date, but yes, I'm able to get FtpWebRequest to work with an ftp proxy by using the old tricks of setting the URL to
"ftp://your.proxy.server/theFileToDownload")"
Then set your network credential to
username="ftpUserName#ftp.realserver.com"
and
password="password".
I remembered doing this back in the bad old WININET days, and sometimes the old tricks are still the best tricks.
YMMV of course.
They claim it is possible to list, listdetails, and download through a proxy. However I could not get this to work with an isa firewall. So I disabled the default proxy in my app.config and added an application rule for ForeFront/ISA client. To do this I created a file c:\programdata\microsoft\firewall client 2004\application.ini with the contents:
[applicationName]
DisableEx=0
Disable=0
NameResolution=R
where applicationName is the running exe minus the .exe extension.

Categories

Resources