developers.de
daenet's .NET Community

User Impersonation in WindowsForms and WPF

Damir Dobric Posts

 

Damir@Phone    



My upcoming sessions:

AppFabric Applications at
NRW Conf 2011
09.Sept.2011 Wuppertal

HTML5 widgets in WP7+
Monodroid
Mobility Day
21.Sept.2011 - Zagreb

AppFabric and WCF sessions at
Advanced Developer Conference
26.-27. Oktober 2011 in Frankenthal

AppFabric Applications, queues, topics and more at
Prio Conference
02. - 03. November 2011 in Meistersingerhalle Nürnberg

AppFabric Applications deep dive hosted by
.NET User Group Frankfurt
17.Nov.2011 18.30-22.30 Microsoft - Bad Homburg 

 

 

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

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
daenet GmbH