Villager
Villager

Reputation: 6689

ASP.NET - Hard question about impersonation

I am building an ASP.NET application. The application uses forms authentication. I am not using roles. However, my account has a flag associated with it that signals I am an administrator.

As an administrator, I would like to call another page in the application via Server.Execute. When I call this page though, I would like to call it as another one of the users in the application.

Is this possible? If so, how do I do it? I know how to run Server.Execute. It's the part of calling it as a different user that is causing me difficulties. I assume this can be done with impersonation. But I'm not sure how to do it.

Thank you!

Upvotes: 2

Views: 354

Answers (1)

Yann Schwartz
Yann Schwartz

Reputation: 5994

Before calling the page you just replace the current principal (admin) with the principal you want to impersonate (we're talking about ASP.Net impersonation, not Windows)

var identity = new GenericIdentity("username");
//Roles get populated for username    
var principal = new RolePrincipal(identity); 
//Current thread now impersonated as "username"
Thread.CurrentThread.CurrentPrincipal = principal;
Server.Execute("whatever.aspx");

Upvotes: 6

Related Questions