Web Applications: Allowing super users to impersonate other users - Is there a design pattern for this?
- In my web application, I'd like to allow super users to impersonate other users.
My Question:
Is there a generally accepted design pattern that I could use to make this happen?
- Generally speaking, I can imagine that I'll need to keep track 开发者_StackOverflowof the current user and the impersonated user inside of the session.
But you can understand that I'd like to minimize the complexity attached to this change.
Incidentally, my application is an ASP.NET MVC 2 application, so if I could take advantage of any existing infrastructure, that would be great.
EDIT: I'm using Forms Authentication.
EDIT: I'll also need to track the fact that a super user is acting on behalf of another user. I'll need to do this for two reasons:
- Logging should log the fact that a super user acted on behalf of another user.
- It's conceivable that the super user would want to return to the impersonation screen to "switch context" and impersonate another user.
EDIT: @Jordão proposed a mostly workable solution. My only concern is the following - If the super user (while impersonating another user) navigates to the home screen, and the top of the screen says 'Hello [User]', I want it to say 'Hello [Impersonated User]' as opposed to 'Hello [Super User]'. And I'm afraid that @Jordão's solution would make the implementation of this screen and other screens with similar requirements more complex.
Don't impersonate other users, but give the super users enough authorizations and exclusive interfaces so that they can act on the other users' data.
If you then have logging or auditing data, you know which user (normal or super) acted on the data.
[Authorize(Roles = "Admin")]
public ActionResult Impersonate(string username)
{
FormsAuthentication.SignOut();
var cookie = FormsAuthentication.GetAuthCookie(username, false);
Response.AppendCookie(cookie);
return RedirectToAction("index");
}
精彩评论