ASP has the ability to handle this through impersonsation. You'll need to implement methods to both set and disable imperonsation.
The way I implemented this was to only decorate the SetImpersonation action with [Authorize] attributes to only allow the Admin user role to impersonate users.
Here's an example for MVC:
To set the impersonation
[Authorize(Roles = (AccountController.Permissions.SUPER_USER))]
public ActionResult ImpersonateUser(string userName)
{
string originalUsername = LoggedInUser.Email;
ApplicationUser impersonatedUser = UserManager.FindByNameAsync(userName).Result;
var impersonatedIdentity = UserManager.CreateIdentityAsync(impersonatedUser, DefaultAuthenticationTypes.ApplicationCookie).Result;
impersonatedIdentity.AddClaim(new Claim("UserImpersonation", "true"));
impersonatedIdentity.AddClaim(new Claim("OriginalUsername", originalUsername));
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, impersonatedIdentity);
return RedirectToAction("Index", "Home");
}
And to revert the impersonation (this action needs to be accessible by the imperonsated user)
public ActionResult RevertImpersonationAsync()
{
if (!HttpContext.User.IsImpersonating())
{
// we could throw an exception here, but it might be more prudent to just silently fail, keeps this feature quiet from snoopers
//throw new Exception("Unable to remove impersonation because there is no impersonation");
return RedirectToAction("Index", "Home");
}
var originalUsername = HttpContext.User.GetOriginalUsername();
var originalUser = UserManager.FindByNameAsync(originalUsername).Result;
var impersonatedIdentity = UserManager.CreateIdentityAsync(originalUser, DefaultAuthenticationTypes.ApplicationCookie).Result;
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, impersonatedIdentity);
return RedirectToAction("Index", "Home");
}