Power BI

Content by Charles Sterling

Impersonation in ASP.NET

I have a coworker who was trying to get impersonation working in ASP.NET and indicated despite setting the Web.config to impersonate:  

<identity impersonate=true userName=Guss password=Password/>

 and he indicated “IT” didnt’ seem to be working….hmm. 

My first question was how do you know your impersonation isn’t working?  He indicated when he ran the following code:

 Label1.Text = My.User.Name.ToString()

The label was still showing his Windows identity not “Guss” which he wanted….Turns out the identity he wanted to change (the and one being displayed) is the Page identity NOT the Windows Identity.

To change this he needed the following code:

Dim id As New System.Security.Principal.GenericIdentity(“Guss”)

Dim p As New System.Security.Principal.GenericPrincipal(id, New String() {“Administrator”})

HttpContext.Current.User = p

Jon Wojtowicz has written a great article on this topis at eggheadcage: