Impersonation across threads in ASP.NET

by Mads Kristensen 31. May 2007 00:22
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" />

Threading


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;
  identity.Impersonate();

  DoSomething();
}

As you can see, it is pretty simple once you know how. * Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

5/31/2007 1:15:32 AM #

Josh Stodola

Hey, thats a great tip.  Thanks for sharing!

Josh Stodola United States |

5/31/2007 1:18:07 AM #

Josh Stodola

Oh yeah, forgot to point out that the title link of your RSS feed is an invalid URL.  Brings me to a 404.

The URL it uses is http://blog.madskristensen.dk/author/

Hope this helps...

Josh Stodola United States |

5/31/2007 6:34:43 AM #

Matt Ellis

Nice tip, but what happens to the thread when it gets returned to the pool? Does the impersonation stop, or does the thread remain impersonating the user? If it's the latter, it's quite a serious security risk...

Cheers
Matt

Matt Ellis |

5/31/2007 6:36:45 AM #

Matt Ellis

PS. I specified my website, but I don't get a link on my name. I'll try setting the country this time, and see what happens.

Matt Ellis United Kingdom |

5/31/2007 6:54:15 AM #

Mads Kristensen

@Josh
Strange about the RSS feed. I can't dublicate the error of the Feedburner feed. I'll look into it. Thanks.

@Matt
Good question. I don't know what happens, but ASP.NET uses the threadpool internally to serve the pages, so I think they get reset once they return to the pool. Otherwise we wouldn't need to pass the identity to new threads.

Mads Kristensen Denmark |

5/31/2007 4:55:15 PM #

trackback

Trackback from DotNetKicks.com

Impersonation across threads in ASP.NET

DotNetKicks.com |

5/31/2007 9:57:31 PM #

Josh Stodola

Hi Mads,

I dont think its a problem with the feedburner feed, rather the one coming out of your syndication handler.  See for yourself...

blog.madskristensen.dk/.../syndication.axd

Hope this helps...

Josh Stodola United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008