5,661,954 members and growing! (15,366 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Impersonation in an asynchronous threaded webpart

By RayBez

Impersonating a user while running asynchronous data retrieval in a webpart
VB, Windows, .NET, Visual Studio, Dev

Posted: 17 May 2006
Updated: 17 May 2006
Views: 10,654
Bookmarked: 8 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.83 Rating: 3.83 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 33.3%
3
2 votes, 66.7%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Speeding up SharePoint Portal Server webparts are relative easy if you understand the asynchronous data retrieval built into the webpart 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 users credentials to be used while running the second asynchronous thread.

Background

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

Some assumptions

  1. You are familiar with creating webparts in VS.Net.
  2. You have the webpart framework installed and running on your development PC.

Using the code

You have to return true for the GetRequiresData method so that the webpart 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 current logged on user is impersonated.

Private Sub ImpersonatedCallbackMethod(ByVal usr As Object)
Impersonate the current 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 time out 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 webpart with the custom message that you want to display when a timeout occurs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

RayBez


I am a programmer for a College in London and the author of Search For More Info. I have been involved in a number of web technologies; ASP.Net being my main language of choice.
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 May 2006
Editor:
Copyright 2006 by RayBez
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project