At first I had some code like this:
WindowsIdentity otherIdentity = // got that from somewhere else
WindowsImpersonationContext context = otherIdentity.Impersonate();
Ldap.DoStuff();
context.Undo();
context.Dispose();
Knowing that WindowsImpersonationContext
implements IDisposable
and Dispose()
also calls Undo()
, I think I should be using using
instead:
using (var context = otherIdentity.Impersonate())
{
// Run as other user
Ldap.DoStuff();
}
Now, ReSharper correctly notices that I am not using context
and suggests to remove the assignment. Does this also work? To what code does it expand at compile time?
using (otherIdentity.Impersonate())
{
Ldap.DoStuff();
}