获取当前登录用户(FullToken Context)
我有一个问题,那就是......我用右键单击启动一个程序->以管理员身份运行。 这意味着该程序正在管理上下文中运行。
WindowsIdentity.GetCurrent().Name;
如果我尝试以这种方式获取用户名,我将获取以管理员身份启动程序的用户..例如“管理员”,但我需要的是当前登录用户的名称,例如: bob
任何人都可以帮帮我吗? :)
I have a Problem, which is... i start a programm with right click -> run as administrator.
Which means the programm is running in an administrative context.
WindowsIdentity.GetCurrent().Name;
if i try to get the user name that way i will get the user that started the programm as admin.. for example "administrator", but what i need is the name of the current logged in user which is for example: bob
Can anybody help me out? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试使用 WMI (System.Management.dll) 来获取
explorer.exe
进程的所有者。这依赖于这样一个事实:资源管理器进程是单个实例,因此您最终不会有多个资源管理器进程使用不同的用户凭据运行的可能性。
You could try using WMI (System.Management.dll) to get the owner of the
explorer.exe
process.This relies on the fact that the explorer process is single instance an so you don't end up with the possibility of having several explorer processes running with different user credentials.
您可能需要为此使用 win32 API。请在此处了解有关 Window Station 和 Desktop 功能的信息:http ://msdn.microsoft.com/en-us/library/ms687107%28v=vs.85%29.aspx
另请参阅此问题:
获取与某个关联的登录 Windows 用户名桌面
You will probably need to use win32 API for that. Read about Window Station and Desktop functions here: http://msdn.microsoft.com/en-us/library/ms687107%28v=vs.85%29.aspx
Also see this question:
Get the logged in Windows user name associated with a desktop
也许您可以以普通用户身份启动,保存用户名,然后以编程方式请求提升:
Windows 7 和 Vista UAC - 在 C# 中以编程方式请求提升
Maybe you could start as normal user, save user name, then programmatically request elevation :
Windows 7 and Vista UAC - Programmatically requesting elevation in C#
所有 .NET 库都会为您提供当前上下文中的用户(在您的情况下为“管理员”)。
如果您想保护您的代码,您可以考虑阅读:. NET框架
All .NET libraries will give you the user from the current context ('Administrator' in your case).
If you are trying to secure your code, you might consider reading about: Security in the .NET framework
1) Cassia 应该能够为您提供当前登录用户的列表,包括 RDC。
2) WMI (SO 答案)
3) PInvoke到 WTSEnumerateSessions
4) 枚举所有实例“explorer.exe”并使用 PInvoke 获取所有者 (OpenProcessHandle)。
这有点hacky。 WMI 也可用于这。
设置 winmgmt< 可能是个好主意如果您决定使用使用 WMI 的解决方案,则将 /a> 作为服务的依赖项。
1) Cassia should be able to give you a list of currently logged in users including RDC.
2) WMI (SO answer)
3) PInvoke to WTSEnumerateSessions
4) Enumerate all instances of "explorer.exe" and get the owner using PInvoke (OpenProcessHandle).
This is a bit hacky. WMI can also be used for this.
It might be a good idea to set winmgmt as a dependency for your service if you decided to go with solution that uses WMI.