In web applications that allow users to create accounts and login, it’s very common for developers to provide some way for an administrator to impersonate / emulate another user. Below is a framework for doing this using the ASP.NET Membership and Profile provider classes. This solution is simply a wrapper around these same classes that will produce cleaner code if you need to include an impersonation feature in your app. I recently used this in a 100K eCommerce application for one of my clients with great success. Hope you like it!
Below are a couple of examples. Scroll down even further for a description of the members of the two CS files: AppMembership.cs and AppProfile.cs.
Example 1
//This line displays the name of the currently logged in user using .NET membership provider:
Response.Write(Membership.GetUser().UserName);
//…and this line does the same thing using the AppMembership wrapper class:
Response.Write(AppMembership.User.UserName);
//Until you start impersonation:
AppMembership.StartImpersonation(“bob”);
//Now, this line displays the name of the impersonated user:
Response.Write(AppMembership.User.UserName);
//…and this line uses the wrapper class to display the name of the currently logged in user:
Response.Write(Membership.GetUser().UserName);
Example 2
//display current user’s username
Response.Write(AppMembership.User.UserName);
//start impersonating Bob
AppMembership.StartImpersonation(“Bob”);
//the same code now displays Bob’s user name
Response.Write(AppMembership.User.UserName);
//display current user’s name (while still impersonating Bob)
Response.Write(Membership.GetUser().UserName);
//end impersonation
AppMembership.EndImpersonation();
//the same code now displays the current user’s name
Response.Write(AppMembership.User.UserName);
File (1 of 2): AppProfile.cs
// Returns boolean value indicating whether a user is currently being impersonated.
static bool IsUserBeingImpersonated
// Returns a MembershipUser object for either:
// a) the logged in user
// b) the user currently being impersonated, regardless of who is logged in.
static MembershipUser User
//Returns the MembershipUser object of the impersonator
static MembershipUser Impersonator
// Returns boolean value to determine:
// a) Whether the logged in user is in the specified role
// b) If impersonating a user, whether the user currently being impersonated is in the specified role
static bool IsUserInRole(string roleName)
// Returns array of strings containing:
// a) Roles the currently logged in user is a member of.
// b) If admin is emulating a user, roles the emulated user is a member of.
static string[] GetRolesForUser(string roleName)
// Start impersonating the specified user
static void StartImpersonation(string UserName)
// Stop impersonating a user
static void EndImpersonation()
File (2 of 2): AppProfile.cs
// Gets the profile of the current user.
// Since this may not always be the logged in user, query the ProfileCommon object
// for the AppMembership.User user, which will always be:
// a) the logged in user
// b) the user currently being impersonated, regardless of who is logged in.
static ProfileCommon UserProfile
// Gets the profile of the specified user.
static ProfileCommon GetUserProfile(string UserName)
// This will cause it to be retrieved and cached again when the UserProfile property is accessed again.
static void Clear()