a developer's notes – a semi-technical web development BLOG

December 17, 2014

Expire All Cookies in C#

Filed under: C# — Duy Nguyen @ 11:27 am
Tags: , ,
public static class CookieUtility
	public static void ExpireAllCookies(HttpRequestBase request, HttpResponseBase response)
		// Get all cookies associated with MYA
		List<HttpCookie> cookies = new List<HttpCookie>();

		var cookieKeysArray = request.Cookies.AllKeys;

		for (int i = 0; i < cookieKeysArray.Length; i++)
			HttpCookie cookie = request.Cookies[cookieKeysArray[i]];

		// Expire all cookies
		foreach (var cookie in cookies)
			if (cookie != null)
				CookieUtility.ExpireCookie(response, cookie);

// From a controller
public virtual ActionResult LogOut(string logOutUrl)
	CookieUtility.ExpireAllCookies(Request, Response);

	return Redirect(logOutUrl);

// From Global.asax
var requestWrapper = new HttpRequestWrapper(Request);
var responseWrapper = new HttpResponseWrapper(Response);
CookieUtility.ExpireAllCookies(requestWrapper, responseWrapper);

December 7, 2014

Adding Cookies to Response, adds to the Request – Removing cookies from the Request

Filed under: ASP.NET MVC,HTML — Duy Nguyen @ 10:42 am
Tags: , , , ,

I don’t know why but when you add cookies to the RESPONSE, it seems to add the same cookie to the REQUEST. I was able to pin point this because I notice in my request, I had a bunch of cookies that I had previously expired from the response. Typically, when you expire cookies, you set the expire date time and add it back into the response.

Also, make sure you remove a cookie from the request AFTER you expire the cookie from the response. Otherwise, the cookie will still be available in the Request later in the process cycle.

November 19, 2013

Visual Studio Performance Load Test – Only First test passes, Subsequent Tests Fails

A big “gotcha” when doing load testing is cached data and stale browser cookies. When examining a load test result, you can spot this symptom when you notice only the 1st (or first few) web tests passes before the test starts to encounter a bunch of failures. Cross check this by making sure your web test can execute many iterations without failure. So if you verified that the web test works with many (if not all) with your data inputs but the load test is producing strange errors, check the following:

1. Percentage of New Users

In your load test file, highlight the Scenario that contains your web test and view its properties. You should find the Percentage of New Users


property and set that value to 100. That will ensure all iterations in your web test will be new users.

2. Clear the Cookie Container

Create a WebTestPlugin and on the PreWebTest method, you can clear the container with the following snippet:

public override void PreWebTest(object sender, PreWebTestEventArgs e)
     e.WebTest.Context.CookieContainer = new System.Net.CookieContainer();

Add this plug-in to ensure at the beginning of each web test, there are no stale cookies from the last virtual user that executed a web test.

Create a free website or blog at WordPress.com.