I also used the code below to access a webpart that displays all sites a user is a member of. This webpart is to appear on the user's my sites...so I needed to impersonate the app pool identity in order for the user to see all the sites he/she has access to.
I wanted to post this to say that the code below definitely impersonated the apppool ID, but I still couldn't access the SP site object. It was weird b/c I could run this code in the catch block while impersonating the apppool ID (administrator) ....
string u = WindowsIdentity.GetCurrent().Name;
string s = base.Context.User.Identity.Name;
output.Write(u + " -- " + s + "
");
and the output would be u = administrator and s = the logged in user <eg. doej>
Patrick states in his blog <blog.u2u.info/.../235.aspx> that at the object model level, SP is always using the account of the logged on user. This certainly would explain the symptoms I was having.
The blog goes on to suggest using a COM object to overcome this hurdle. That is not an option for us...
At any rate, I have added this one line of code from you/Todd/Jeff Goddard and after several hours of testing, it appears to access the SP objects with the impersonated app pool account.
try
{
this.UseAppPoolIdentity();
WindowsIdentity.Impersonate(WindowsIdentity.GetCurrent().Token);
.....
}
catch
{}
finally
{this.ReturnToImpersonatingCurrentUser();}