简体   繁体   English

Thread.CurrentPrincipal.Identity.Name 从 WPF 为空

[英]Thread.CurrentPrincipal.Identity.Name is empty from WPF

EDIT编辑

The simple question is, how can I get Thread.CurrentPrincipal.Identity.Name to have the current user logon in WPF?简单的问题是,如何让Thread.CurrentPrincipal.Identity.Name让当前用户登录 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:我正在尝试调用一个现有方法(不是在任何类型的服务中;只是 POCO 中的一个方法),它通过以下方式检索当前用户:

Thread.CurrentPrincipal.Identity.Name

This code was written by someone else, and (presumably) works with his ASP.NET MVC project.此代码是由其他人编写的,并且(可能)适用于他的 ASP.NET MVC 项目。 I'm trying to call this same method from WPF, and Name is now blank.我正在尝试从 WPF 调用相同的方法,并且名称现在为空白。

Is there anything I can do about this?对此我能做些什么吗?

Set Thread.CurrentPrincipal to new WindowsPrincipal(WindowsIdentity.GetCurrent()) .Thread.CurrentPrincipal设置为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.编辑:我还应该提到 AppDomain 上的SetThreadPrincipalSetPrincipalPolicy方法。 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.如果不调用此方法,每个新线程将再次以 GenericPrincipal 开始。

Try尝试

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

or:或者:

Environment.UserName

This also worked for me:这也对我有用:

Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在请求之间以空字符串形式返回的ADFS3.0 Thread.CurrentPrincipal.Identity.Name - ADFS3.0 Thread.CurrentPrincipal.Identity.Name returned as empty string between requests Thread.CurrentPrincipal.Identity.Name与OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name - Thread.CurrentPrincipal.Identity.Name vs OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name CurrentPrincipal.Identity.Name为空 - CurrentPrincipal.Identity.Name is blank Thread.CurrentPrincipal.Identity和Active Directory - Thread.CurrentPrincipal.Identity And Active Directory 在ASP.NET MVC 6(vNext)中Thread.CurrentPrincipal Identity.Name为null - Thread.CurrentPrincipal Identity.Name is null in asp.net mvc 6 (vNext) 为什么Thread.CurrentPrincipal.Identity.IsAuthenticated始终为false? - Why Thread.CurrentPrincipal.Identity.IsAuthenticated is always false? 从IAuthorizationPolicy设置Thread.CurrentPrincipal? - Set the Thread.CurrentPrincipal from IAuthorizationPolicy? 在测试中为WPF应用程序设置CurrentPrincipal - Setting CurrentPrincipal for a WPF application from within a test 用户身份标识名称在HttpContext中为空 - User Identity Identity Name is Empty from HttpContext Thread.CurrentPrincipal 是 ClaimsIdentity 返回 false 以及如何从 Thread.CurrentPrincipal 获取声明 - Thread.CurrentPrincipal is ClaimsIdentity returns false and how to get the claims from Thread.CurrentPrincipal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM