[英]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 上的SetThreadPrincipal和SetPrincipalPolicy方法。 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.