Click here to Skip to main content
13,190,994 members (56,617 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 17 May 2006

Impersonation in an asynchronous threaded WebPart

, 17 May 2006
Rate this:
Please Sign up or sign in to vote.
Impersonating a user while running an asynchronous data retrieval in a WebPart.


Speeding up SharePoint Portal Server Web Parts are relative easy if you understand the asynchronous data retrieval built into the Web Part framework. Just one problem, the second asynchronous thread runs under the system account's permissions.

This article is not on how to get asynchronous data retrieval working (even though the code is included), but rather on how to get the current logged on user's credentials to be used while running the second asynchronous thread.


While developing a treeview navigation Web Part, I had the opportunity to get my hands dirty with the asynchronous data retrieval functionality built into the Web Part framework. It was easy enough to get to work. The only problem that I found was that when I wanted to determine (during the asynchronous routine) whether the logged on user had permissions to view an area, it would always return True.


  1. You are familiar with creating Web Parts in VS.NET.
  2. You have the Web Part framework installed and running on your development PC.

Using the Code

You have to return True for the GetRequiresData method so that the Web Part will know to use asynchronous data retrieval.

Public Overrides Function GetRequiresData() As Boolean
    Return True
End Function

As you have indicated that you want to use asynchronous data retrieval, the framework will next check the GetData method to see which callback method to call. You use RegisterWorkItemCallback to register a method of type Threading.WaitCallback. No value is returned, but you have to pass a value of type Object to the callback method.

Public Overrides Sub GetData()
    RegisterWorkItemCallback(New Threading.WaitCallback(_
       AddressOf ImpersonatedCallbackMethod), Nothing)
End If

Now that the asynchronous data retrieval is ready, you have to get the callback method configured so that the currently logged on user is impersonated.

Private Sub ImpersonatedCallbackMethod(ByVal usr As Object)

Impersonate the currently logged on user:

Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity 
currentWindowsIdentity = CType(System.Security.Principal.WindowsIdentity.GetCurrent, _
impersonationContext = currentWindowsIdentity.Impersonate()

Run any script that needs to use the permissions of the current user, and finally remove the impersonation.

    'Remove the impersonation
End Sub

Points of Interest

The asynchronous thread will timeout after the default 7 seconds, and is controlled by the WebPartWorkItem element in the web.config. You will therefore have to add a RenderWorkItemTimeout method to the Web Part with the custom message that you want to display when a timeout occurs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Program Manager
South Africa South Africa
Stuck with a desk job by day, but an entrepreneur, software developer and internet marketer by night. I own a South African retail site - - and have been involved in a number of web ecommerce projects. ASP.Net being my main technology of choice while building on MOSS 2010.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171016.2 | Last Updated 17 May 2006
Article Copyright 2006 by RayBez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid