C#: Enable Cookie Container on System.Net.WebClient (for Authentication)

As I told on the previous post the WebClient can be easily extended to add more functionality such in this case the Cookie Container, and let use it to authenticate versus the web pages that are protected using an authenticated cookie:

public class CookieWebClient : WebClient
{
	public CookieContainer CookieContainer { get; private set; }

	/// <summary>
	/// This will instanciate an internal CookieContainer.
	/// </summary>
	public CookieWebClient()
	{
		this.CookieContainer = new CookieContainer();
	}

	/// <summary>
	/// Use this if you want to control the CookieContainer outside this class.
	/// </summary>
	public CookieWebClient(CookieContainer cookieContainer)
	{
		this.CookieContainer = cookieContainer;
	}

	protected override WebRequest GetWebRequest(Uri address)
	{
		var request = base.GetWebRequest(address) as HttpWebRequest;
		if (request == null) return base.GetWebRequest(address);
		request.CookieContainer = CookieContainer;
		return request;
	}
}

And then you can use it as follow:

using (var client = new CookieWebClient())
{
	var loginData = new NameValueCollection
	{
		{ "UserName", "TestUser" },
		{ "Password", "MyPassword" }
	};

	// Login into the website (at this point the Cookie Container will do the rest of the job for us, and save the cookie for the next calls)
	client.UploadValues("http://domain.com/Account/LogOn", "POST", loginData);

	// Call authenticated resources
	client.UploadString("http://domain.com/ProtectedArea/MyProtectedResource", "POST", "some data");
}

CookieWebClient takes also a CookieContainer as parameter, to let you control the cookie container outside, and passing over different CookieWebclient instantiation.

How to use a Local Storage Resource

The Windows Azure Managed Library provides classes for accessing the local storage resource from within code that is running in a role instance.

You will just need to retrieve the full path of a named local storage, and then you can store any file you want. To retrieve the full path, you simple need this line of code:

RoleEnvironment.GetLocalResource("MainLocalStorage").RootPath

How to add a Windows Azure Cloud Service Project on an existing Web Project

You can accomplish this easy step just right-clicking on the Web Project and then select the menu entry “Add Windows Azure Cloud Service Project”

How to add a Windows Azure Cloud Service Project on an existing Web Project

How to know if your code is running on the Windows Azure compute emulator

To understand whether the role instance is running in the Windows Azure compute emulator, you simply need to check this static variable:

RoleEnvironment.IsEmulated

Namespace: Microsoft.WindowsAzure.ServiceRuntime
Assembly: Microsoft.WindowsAzure.ServiceRuntime (in Microsoft.WindowsAzure.ServiceRuntime.dll)

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.isemulated.aspx

How to know if your code is running in the Windows Azure environment

To understand whether the role instance is running in the Windows Azure environment you simply need to check this static variable:

RoleEnvironment.IsAvailable

Namespace: Microsoft.WindowsAzure.ServiceRuntime
Assembly: Microsoft.WindowsAzure.ServiceRuntime (in Microsoft.WindowsAzure.ServiceRuntime.dll)

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.isavailable.aspx

C#: Enable Automatic Decompression on System.Net.WebClient

The WebClient class is really easy to use, but actually doesn’t provide so much control over the underline request, or so it seems, but we can still inherits from it to get the control over the WebRequest that the WebClient using.

With this in mind I’ve could extend the WebClient enabling the powerfull AutomaticDecompression of the HttpWebRequest, and getting compressed web resources:

public class AutomaticDecompressionWebClient : WebClient
{
	protected override WebRequest GetWebRequest(Uri address)
	{
		var request = base.GetWebRequest(address) as HttpWebRequest;
		if (request == null) throw new InvalidOperationException("You cannot use this WebClient implementation with an address that is not an http uri.");
		request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
		return request;
	}
}