Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.CurrentPrincipal.Identity.Name is empty from WPF

Tags:

c#

wpf

EDIT

The simple question is, how can I get Thread.CurrentPrincipal.Identity.Name to have the current user logon in WPF?

END EDIT

I'm trying to call an existing method (not in any sort of service; just a method in a POCO) that retrieves the current user with:

Thread.CurrentPrincipal.Identity.Name

This code was written by someone else, and (presumably) works with his ASP.NET MVC project. I'm trying to call this same method from WPF, and Name is now blank.

Is there anything I can do about this?

like image 370
Adam Rackis Avatar asked Jul 14 '11 20:07

Adam Rackis


2 Answers

Set Thread.CurrentPrincipal to new WindowsPrincipal(WindowsIdentity.GetCurrent()). You'll then reliably have the current principal for the lifetime of that thread. You'll have to repeat this on any other threads you spin up.

EDIT: I should also mention the SetThreadPrincipal and SetPrincipalPolicy methods on AppDomain. This should be done on application startup, and new threads created will now see this principal by default. If this method isn't called, every new thread will start out with GenericPrincipal again.

like image 159
Andy Avatar answered Nov 03 '22 04:11

Andy


Try

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

or:

Environment.UserName
like image 6
Mrchief Avatar answered Nov 03 '22 05:11

Mrchief