Thread.CurrentPrincipal.Identity.Name is empty from WPF

后端 未结 3 1785
天涯浪人
天涯浪人 2020-12-28 08:30

EDIT

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

相关标签:
3条回答
  • 2020-12-28 08:59

    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.

    0 讨论(0)
  • 2020-12-28 09:02

    Try

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

    or:

    Environment.UserName
    
    0 讨论(0)
  • 2020-12-28 09:22

    This also worked for me:

    Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    
    0 讨论(0)
提交回复
热议问题