Skip to content

Instantly share code, notes, and snippets.

@camt
Created January 27, 2016 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camt/21a3334a65678dfc25a8 to your computer and use it in GitHub Desktop.
Save camt/21a3334a65678dfc25a8 to your computer and use it in GitHub Desktop.
A simple proxy hack to facilitate routing traffic from an app via a proxy. Build BasicProxy as a standalone DLL, and then add the we.config stuff to your app's web.config.
using System;
using System.Net;
using System.Windows.Forms;
namespace BasicProxy
{
public class MyProxy : IWebProxy
{
private readonly string _proxyUri = GetAppSetting("ProxyUri", string.Empty);
private readonly string _proxyUsername = GetAppSetting("ProxyUsername", "user");
private readonly string _proxyPassword = GetAppSetting("ProxyPassword", "password");
private readonly string _proxyDomain = GetAppSetting("ProxyDomain", string.Empty);
public ICredentials Credentials
{
get
{
if (string.IsNullOrEmpty(_proxyDomain))
{
return new NetworkCredential(_proxyUsername, _proxyPassword);
}
return new NetworkCredential(_proxyUsername, _proxyPassword, _proxyDomain);
}
set { }
}
public Uri GetProxy(Uri destination)
{
return new Uri(_proxyUri);
}
public bool IsBypassed(Uri host)
{
return false;
}
public static string GetAppSetting(string key, string defaultValue = "")
{
var asr = new System.Configuration.AppSettingsReader();
string result;
try
{
result = asr.GetValue(key, typeof(string)).ToString();
}
catch (Exception)
{
result = defaultValue;
}
return result;
}
}
}
<appSettings>
<add key="ProxyUri" value="proxyUri" />
<add key="ProxyUsername" value="proxyUsername" />
<add key="ProxyPassword" value="proxyPassword" />
<add key="ProxyDomain" value="" /> <!-- ignored if empty string -->
</appSettings>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type = "BasicProxy.MyProxy, BasicProxy" />
</defaultProxy>
</system.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment