从UAC开始时获取当前用户名 [英] Get current user name when starting with UAC

查看:67
本文介绍了从UAC开始时获取当前用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的设置具有嵌入式清单,该清单在应用程序启动之前触发UAC.(应用程序以管理员用户身份运行).但是,如果安装程序需要安装.NET Framework,则必须在重新启动后继续安装.因此,我们必须在当前用户的RunOnce中创建一个注册表项.

Our setup has an embedded manifest that triggers the UAC before the application starts. (The applications runs as an admin user). However, if the setup needs to install the .NET Framework, we have to continue the setup after a reboot. For this reason, we have to create a registry key in the current user's RunOnce.

不幸的是,HKEY_CURRENT_USER指向管理员的注册表.我们需要找出当前登录并开始安装的用户.(普通用户单击了setup.exe,ADMIN在UAC提示符中输入了他的详细信息.我们需要找出该用户是谁)

Unfortunatly, HKEY_CURRENT_USER points to the Administrator's registry. We need to find out the user that is currently logged in and started the installation. (Th normal USER clicked the setup.exe, an ADMIN entered his details in the UAC prompt. We need to find out who the USER was)

我已经尝试了所有常用方法(Environment.UserName,WindowsIdentity.GetCurrent())

I've tried all the usual methods (Environment.UserName, WindowsIdentity.GetCurrent())

谢谢!

推荐答案

您可以使用源代码,在其终端服务API中使用此功能.通话应该是一样的.您也可以在此处中找到.

You can use the LsaEnumerateLogonSessions function to retreive what you need. However, it is a winapi C function call. If you need a managed version of it, I belive you can look at the source code for Cassia, which uses this function in its terminal services API. The call should be the same. You can also look here.

您还可以使用 NetWkstaUserEnum WINAPI功能.您可以在此处

Also you can use the NetWkstaUserEnum WINAPI function. You can find a managed wrapper for it here

对于Cassia库,此代码可以正常工作:

With Cassia library this code works fine:

ITerminalServicesManager manager = new TerminalServicesManager();
ITerminalServicesSession session = manager.CurrentSession;

string userInfo = session.DomainName + "\\" + session.UserName;
NTAccount account = session.UserAccount;

这篇关于从UAC开始时获取当前用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆