Using A Cookie-Aware WebClient To Persist Authentication In ASP.NET MVC

Earlier this year, I was working on an iOS Application that interacted with a running ASP.NET MVC site and the need arose for the iPhone user to authenticate so that they could access some of the Actions within a Controller decorated with the [Authorize] attribute.

This post will cover how to create a simple cookie-aware extension of the WebClient class that will authenticate and persist this authentication for the duration of the WebClient to allow access to secure areas of your MVC Application without the need for re-authenticating for each request.

The Problem

You are using a very basic ASP.NET MVC Application with integrated Forms Authentication and the need arises for you to authenticate and access multiple controller actions, which require authentication, using a single WebClient instance.

The Solution

In order to handle this issue, we will need to create a custom WebClient class that will persist our authentication token for the duration of our visit based on parameters that were passed in. This can be accomplished by creating a class that will inherit from the WebClient class and will have a specific container to house the authentication cookie:

  1. // Cookie-Aware WebClient  
  2. public class CookieAwareWebClient: WebClient  
  3. {  
  4.     // An aptly named container to store the Cookie  
  5.     public CookieContainer CookieContainer  
  6.     {  
  7.         get;  
  8.         private set;  
  9.     }  
  10.     public CookieAwareWebClient()  
  11.     {  
  12.         CookieContainer = new CookieContainer();  
  13.     }  
  14. }  
In its current state – this fails to actually do anything except create an extra CookieContainer property that we can’t really do anything with as we will need to determine a way to actually populate the authentication cookie and store it within the WebClient from any of the Requests that are made through the client itself.

This can be handled by adding the following snippet of code within the CookieAwareWebClient class:
  1. protected override WebRequest GetWebRequest(Uri address)  
  2. {  
  3.     // Grabs the base request being made   
  4.     var request = (HttpWebRequest) base.GetWebRequest(address);  
  5.     // Adds the existing cookie container to the Request  
  6.     request.CookieContainer = CookieContainer;  
  7.     return request;  
  8. }  
The above code will associate the current Request that is being made from the WebClient and add the appropriate cookie to handle any future requests made by the client.

Digging in the Cookie Jar

The following example details how to use the newly created CookieAwareWebClientinstance within an MVC environment by allowing the current user (in this instance from an iOS Application built through Xamarin) to authenticate using the standard AccountController available in ASP.NET MVC and then call a method from a Controller that is decorated with the [Authorize] Attribute.

Let’s take a look at exactly what we want to access through our iOS call:
  1.    [Authorize]  
  2.    public class SecureController: Controller  
  3. {  
  4.     // A secure method that will be accessed through   
  5.     // your iOS Application  
  6.     public ActionResult YourSecureMethod(string data)  
  7.     {  
  8.         return Content(String.Format("{0} was passed in.", data);  
  9.     }  
  10. }  
As you can see there isn't really anything special about the Controller or the Action besides the [Authorize] decoration at the Controller level, which is great because we don’t really want to have to do anything special to handle this functionality.

Within your iOS application, we are going to make two calls (the first to authenticate and the second to make the actual call itself) within an instance of the custom WebClient:
  1. // Create an instance of your new CookieAware Web Client  
  2. using(var client = new CookieAwareWebClient())  
  3. {  
  4.     // Authenticate (username and password can be either  
  5.     // hard-coded or pulled from a settings area)  
  6.     var values = new NameValueCollection  
  7.     {  
  8.         {  
  9.             "UserName", username  
  10.         },  
  11.         {  
  12.             "Password", password  
  13.         }  
  14.     };  
  15.     // Perform authentication - after this has been   
  16.     // performed the cookie will be stored within the Web Client  
  17.     client.UploadValues(new Uri(""), "POST", values);  
  18.     // Authentication worked! Access the secure area (you   
  19.     // can use a variety of methods here such as UploadFile,  
  20.     // UploadValues, etc.)  
  21.     client.UploadString(new Uri(""), "POST""Example Message");  
  22. }  
And that’s really all you need as you could call a multitude of other Controller Actions within the context of your WebClient. It should be noted that your authentication values will only persist during the using statement and any further attempts to access your application outside of it will require additional authentication.

More Chocolate Chips to Add to the Cookies

This is just one of the many examples of the Web Client being extended to assist with a very simple requirement that previous several redundant calls from being made within a simple Request. Another feature that I found to be incredibly helpful is implementing a Timeout mechanism for your WebClient.

Timing out can be essential when dealing with requests from iOS devices, as most users don’t want to sit there and wait indefinitely for a password that they accidentally left the CAPS LOCK key on for to be rejected. For this reason, implementing a timeout can put a stop to a request after a specific amount of time and notify the user that they will need to retry their request.

All you will need to do to handle this will be to add an integer property to the WebClient class:
  1. // An optional timeout property  
  2. private int? _timeout = null;   
  3. public int? Timeout   
  4. {  
  5.    get { return _timeout; }  
  6.    set { _timeout = value; }  
  7. }  
Along with a method on your WebClient to handle setting it:
  1. public void SetTimeout (int timeout)   
  2. {  
  3.    _timeout = timeout;  
  4. }  
And then finally, you’ll need to actually use it when your WebRequests are being made, so you can add the logic in there as well:
  1. protected override WebRequest GetWebRequest(Uri address)  
  2. {  
  3.     var request = (HttpWebRequest) base.GetWebRequest(address);  
  4.     request.CookieContainer = CookieContainer;  
  5.     // Sets a timeout for the Request if the   
  6.     // timeout property is available  
  7.     if (_timeout.HasValue)  
  8.     {  
  9.         request.Timeout = _timeout.Value;  
  10.     }  
  11.     return request;  
  12. }  
So after all that, now you would just need to set the _timeout property (if needed) after declaring your WebClient:
  1. using(var client = new CookieAwareWebClient())  
  2. {  
  3.     // Times out after 5 minutes  
  4.     client.SetTimeout(5 * 60 * 1000);  
  5.     // I found that on iOS devices that the KeepAlive  
  6.     // header can be helpful (but it is likely not   
  7.     // completely necessary in most instances)  
  8.     client.Headers[HttpRequestHeader.KeepAlive] = "true";  
  9.     // Additional code omitted for brevity  
  10. }  
Too. Much. Cookie

If this post made you hungry and you are too overwhelmed or are edging towards a cookie-coma, don’t fret. I won’t make you go through the extended trouble of copy-pasting the entire class above from all of those separate snippets.

You can download the class from GitHub below in case the need ever arises that you should require something even remotely like it or you would want to extend it further: 

Download the CookieAwareWebClient Class from GitHub.