Get past that proxy server – Method 2 (using configuration)
by Hans on 2012/05/16 09:38
in Categories: C#


This is second of two posts dealing with authenticating and getting past proxy servers. This method will show how to get past the proxy server using configuration. The basic assumption here is that you have a piece of .Net software that accesses external web resources. In addition you don't have access to the code to add proxy authentication or you simply don't want to change the code.

I must just add this post and code snippets assumes a certain amount of proficiency in C#. This is NOT the dummies guide to C#.

Method 2: Config your way past the proxy server

Ok so here is a very simple piece of code that calls a web URL and returns the response as a string. It will be similar to an existing piece of code you have that does web requests. Note the absence of code to provide proxy authentication.

public string GetResponse( string uri )

{

System.Net.HttpWebRequest request = null;

System.Net.HttpWebResponse response = null;

request = ( System.Net.HttpWebRequest ) System.Net.WebRequest.Create( uri );

request.Method = "GET";

request.KeepAlive = false;

request.Timeout = 60000;

response = ( System.Net.HttpWebResponse ) request.GetResponse();

System.IO.StreamReader reader = new System.IO.StreamReader( response.GetResponseStream() );

string toreturn = reader.ReadToEnd();

reader.Close();

return toreturn;


}

OK so instead of modifying the above code to include proxy authentication we modify the web config file or the app config depending on the type of .Net application you have in front of you

Step 1. Create a library project and add a class called "ProxyConfiguration.cs". The code is shown below.

 
The purpose of this class is allow us to define via config our proxy url, username, password and domain details. We create this configuration class which extends the ConfigurationSection class and include the properties we want to define in the application config file. Real easy stuff.

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace SaxonSystems.Configuration
{

public class ProxyConfiguration : ConfigurationSection

{


private static ProxyConfiguration _Config = ( ProxyConfiguration ) System.Configuration.ConfigurationManager.GetSection( "SaxonSystemsProxyConfiguration" );

 

public static ProxyConfiguration Settings

{

get

{

     return _Config;

}

}

 

[ConfigurationProperty( "Uri", IsRequired = true )]

public string Uri

{

get

{

     return this[ "Uri" ] as string;

}

}


[ConfigurationProperty( "Password", IsRequired = true )]

public string Password

{

get

{


return this[ "Password" ] as string;

}

}

 

[ConfigurationProperty( "UserName", IsRequired = true )]

public string UserName

{

get

{


return this[ "UserName" ] as string;

}

}

 

[ConfigurationProperty( "Domain", IsRequired = true )]

public string Domain

{

Get

{


return this[ "Domain" ] as string;

}

}

}

}
 
Step 2. Add a class called "AuthenticatedProxy.cs". The code is shown below.
 
using System;
using System.Collections.Generic;
using System.Text;
using SaxonSystems.Configuration;

namespace SaxonSystems.Net
{

//The important thing here is that this code extends "System.Net.IWebProxy". We are creating a proxy authentication class.

public class AuthenticatedProxy : System.Net.IWebProxy

{

public System.Net.ICredentials Credentials

{

get

{

//note the below method has at 3 overloads. Use the one that best suits your requirement. DO NOT do this "domainname\username" and think

//you can combine domain name and username IT DOES NOT WORK


//note I have added a second configuration class that allows us to specify credentials in the config file. No point hard coding credentials !

return new System.Net.NetworkCredential( ProxyConfiguration.Settings.UserName, ProxyConfiguration.Settings.Password, ProxyConfiguration.Settings.Domain);

}

set

{

}

}

 

public Uri GetProxy( Uri destination )

{

      return System.Net.WebRequest.GetSystemWebProxy().GetProxy( destination );

}

public bool IsBypassed( Uri host )

{

       return false;

}

}

}
 
Step 3. Ok now we need to modify the config file of your .net application and add the class library you created and let the .net framework do its magic.

This is what the config file looks like. The important bit is where we reference the class library we created. See below.

<module type="SaxonSystems.Net.AuthenticatedProxy, SaxonSystems.Net, Version=1.0.0.0, Culture=neutral"/>

What happens here is that when the applications run the .net framework automatically loads the class library defined in the module element and uses this to authenticate against the proxy server. Clever Yes? Definitely !

Note too how I added the ProxyConfiguration class into the config file so we can dynamically change the proxy authentication details

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>

<section name="SaxonSystemsProxyConfiguration" type="SaxonSystems.Net.ProxyConfiguration, SaxonSystems.Net, Version=1.0.0.0, Culture=neutral" requirePermission="false" restartOnExternalChanges="true"/>

</configSections>

<SaxonSystemsProxyConfiguration UserName="myusername" Password="mypassword" Uri="http://isa:8080" />

<system.net>

<defaultProxy enabled="true" useDefaultCredentials="false">

<module type="SaxonSystems.Net.AuthenticatedProxy, SaxonSystems.Net, Version=1.0.0.0, Culture=neutral"/>

</defaultProxy>

</system.net>

</configuration>


Step 4. Compile your class library and add the dll to the bin folder of your application


I hope this helps. Until next time Happy hacking !!


Post a Comment

Your Name:*  OR Screen Name:
Email Address:*
Comment:*
Post your Comment

    Note. All comments are sent to a moderator for approval prior to appearing on this page.

    © 2012 Saxon Systems. All rights reserved.Terms of Use  Privacy Policy  Contacts