User Impersonation in WindowsForms and WPF

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




Following example shows how to enforce interactive user in Windows Forms or WPF applications to become WindowsPrincipal, which can be read by Thread.CurrentPricipal:

  // This is identity of the user interactivelly logged on.
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

  // This would be required if you would logon user by username and password.
  // This example shows how to impersonate some other user than the interactive one.
WindowsImpersonationContext ctx = currentIdent.Impersonate();

  // This two calls make the all magic.

  WindowsPrincipal wP = new WindowsPrincipal(currentIdent);

Thread.CurrentPrincipal = wP;

Posted Mar 19 2010, 10:48 AM by Damir Dobric is a .Net Community Blog powered by daenet GmbH.