ASP.NET MVC Creating a impersonated user.
Posted
by Malcolm
on Stack Overflow
See other posts from Stack Overflow
or by Malcolm
Published on 2010-03-30T02:38:42Z
Indexed on
2010/03/30
2:43 UTC
Read the original article
Hit count: 342
asp.net-mvc
Hi,
I have a MVC app where I have a User class and the user can also impersonate another user(Admin users only).
So I have this code below that authenticates the request and instantiates my version of a User class.
It then tries to get the impersonated user from the Session object but Session is not available in this method in the global.asax.
Hope this makes sense.
How else could I do this?
protected void Application_OnAuthenticateRequest(object sender, EventArgs e) { IMylesterService service = ObjectFactory.GetInstance();
if (Context.User != null)
{
if (Context.User.Identity.IsAuthenticated)
{
User user = service.GetUser(Context.User.Identity.Name);
if (user == null)
throw new ApplicationException("Context.user.Identity.name is not a recognized user");
User impersonatedUser = (User)this.Session["ImpersonatedUser"];
if (impersonatedUser == null)
user.ImpersonatedUser = user;
else
user.ImpersonatedUser = impersonatedUser;
System.Threading.Thread.CurrentPrincipal = Context.User = user;
return;
}
}
User guest = service.GetGuestUser();
guest.ImpersonatedUser = guest;
System.Threading.Thread.CurrentPrincipal = Context.User = guest;
}
© Stack Overflow or respective owner