Some web applications need to impersonate a user account for one reason or another. This is done from web.config and could look like this:

<identity impersonate="true" />


The impersonation works fine, but if you create new threads manually you would lose the impersonation if you don’t move it along to the new thread. This gave me a severe headache before I figured out how to pass the impersonation to the newly created thread.  

What you need to do, is to pass the WindowsIdentity object to the new thread and from there impersonate again manually.  Here is an example of how to do it using the ThreadPool:

public void StartAsyncCall()
  System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  ThreadPool.QueueUserWorkItem(RunAsyncCall, identity);

private void RunAsyncCall(object stateInfo)
  System.Security.Principal.WindowsIdentity identity = (System.Security.Principal.WindowsIdentity)stateInfo;


As you can see, it is pretty simple once you know how.


ASP.NET has an outgoing connection limit which is set to 2 connections by default. It means that the ASP.NET process does not create more than 2 concurrent connections simultaneously, but instead queues them up.

Consider the scenario where you call 4 web services asynchronously from the same method. What happens is that the first two starts to execute, while the next two waits for the first two to finish. The same thing happens for HttpRequests.

That is a waste of time in most cases, so you can raise the connection limit to allow more simultaneous reqests in the web.config.

     <add address="*" maxconnection="8"/>

Every asynchronous request is started in its own thread, so you have to be careful not to raise the limit too far. The more CPU’s or cores the web server have, the higher can you set the connection limit.

You can read more about asynchronous requests and the connection limit on MSDN.


I’ve been fiddling with accessibility standards and validation today. It’s something I’ve been doing for several years now on many different types of web applications. So today it was time for my blog to go through validation and it almost did.

Section 508

First, I started out with the Section 508 standard, which as far as I know only applies in the US, but nevertheless it is an accessibility standard and I know it is much more forgiving than WAI. It passed the Section 508 without I had to change anything, so that was easy.


Then it was time for the WAI validation. It is not as forgiving as Section 508 and in Europe all public websites have to be valid WAI double A, which is the medium accessibility grade. I ran the Bobby validator and it didn’t even validate for single A. However, that was an easy fix.


Then I moved on to WAI double A and ran into serious trouble. It validated except for this rule: Do not use the same link phrase more than once when the links point to different URLs. This is impossible to fix, because at the bottom of each post there is a Permalink, Post RSS and Comments link which are almost the same for each post even though they point to different locations. Also, some tags are the same as a category, but again, they point to different locations.

Sticking with WAI A

So, I was left with a choice. I could change the text of the links or accept that my blog does not validate WAI double A. The problem is, that you cannot have triple A valid markup if you don't pass the double A validation. I came to the conclusion that renaming the links would be ugly and do more harm than not validating, so it all ended right there. I have to settle for a single A and a Section 508 valid website. It could be worse.

Other validators

Then I saw that W3C has released a beta version of their new markup validator, so I decided to give it a go. As I hoped, my blog validated perfectly XHTML 1.0 Transitional. I was then pointed to the Semantic Data Extractor which exposes the semantics of a webpage to give you a clear picture of how the text is structured. Here is the semantics of my blog. It looks quite nice I guess.

I like validating markup and always have. It has some meditative quality about it.


Some people have asked me how BlogEngine.NET displays a dropdown list of countries when no source XML file is present. The simple answer is that you don’t need any external list to bind to from C#, you can instead use the CultureInfo class.

Consider that you have the following dropdown list declared in an ASP.NET page:

<asp:DropDownList runat="server" ID="ddlCountry" />

Then from code-behind, call this method which binds the countries alphabetically to the dropdown:

public void BindCountries()
  System.Collections.Specialized.StringDictionary dic = new System.Collections.Specialized.StringDictionary();
  System.Collections.Generic.List<string> col = new System.Collections.Generic.List<string>();

  foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
    RegionInfo ri = new RegionInfo(ci.LCID);
    if (!dic.ContainsKey(ri.EnglishName))
      dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant());

    if (!col.Contains(ri.EnglishName))


  ddlCountry.Items.Add(new ListItem("[Not specified]", ""));
  foreach (string key in col)
    ddlCountry.Items.Add(new ListItem(key, dic[key]));

  if (ddlCountry.SelectedIndex == 0 && Request.UserLanguages != null && Request.UserLanguages[0].Length == 5)
    ddlCountry.SelectedValue = Request.UserLanguages[0].Substring(3);

The method first adds all the countries from the CultureInfo class to a dictionary and then sorts it alphabetically. Last, it tries to retrieve the country of the browser so it can auto-select the visitors country. There might be a prettier way to sort a dictionary, but this one works.


Normally, I don’t dedicate an entire post to a remote link, but this is no normal case!

Douglas Crockford, whom I’ve never heard of before, did a speak recently at the 2007 FrontEnd Engineering Summit about code quality and the kind folk at Yahoo recorded it for everyone to see.

It’s a long video but it is worth watching it from start to finish. He has a huge insight into the history of software development and makes some really good analogies that help to understand what he talks about.

If you only watch one video this year, then this video should be the one.

Check out Douglas Crockford on code quality.