Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Background

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.

Pre-requisites

  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, _
                               System.Security.Principal.WindowsIdentity)
impersonationContext = currentWindowsIdentity.Impersonate()

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

    'Remove the impersonation
    impersonationContext.Undo() 
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.

License

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

About the Author

RayBez
Program Manager Ashrays.co.za
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 - Ashrays.co.za - and have been involved in a number of web ecommerce projects. ASP.Net being my main technology of choice while building on MOSS 2010.
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 17 May 2006
Article Copyright 2006 by RayBez
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid