Impersonation.RevertToSelf()

In a SharePoint web.config you will find the line that causes all server code to be run in the context of the requesting client. So if you need your webpart to e.g. look up a user in the active directory (i.e. on the domain controller) you have a double hop issue.

You can use credentials of the app pool account to overcome this using the following code snippet:

// Uncomment for pre .net 3.5 > delegate void Action();  
static void DelegateToIIS(Action command)  
{
    if (!WindowsIdentity.GetCurrent().IsSystem)
    {
        var ctx = WindowsIdentity.Impersonate(IntPtr.Zero);
        try
        {
            command();
        }
        finally
        {
            ctx.Undo();
            ctx.Dispose();
        }
    }
    else
        command();
}
Google
m@kli.dk @klinkby RSS feed  GitHub