Created
January 27, 2016 00:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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