简体   繁体   English

获取当前登录的用户(FullToken上下文)

[英]Getting the current logged in user (FullToken Context)

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 如果我尝试以这种方式获取用户名,则将以管理员身份启动启动程序的用户。例如“ administrator”,但我需要的是当前已登录用户的名称,例如:bob

Can anybody help me out? 有人可以帮我吗? :) :)

You could try using WMI (System.Management.dll) to get the owner of the explorer.exe process. 您可以尝试使用WMI(System.Management.dll)来获取explorer.exe进程的所有者。

string GetExplorerUser()
{
    var query = new ObjectQuery(
        "SELECT * FROM Win32_Process WHERE Name = 'explorer.exe'");

    var explorerProcesses = new ManagementObjectSearcher(query).Get();

    foreach (ManagementObject mo in explorerProcesses)
    {
        string[] ownerInfo = new string[2];
        mo.InvokeMethod("GetOwner", (object[])ownerInfo);

        return String.Concat(ownerInfo[1], @"\", ownerInfo[0]);
    }

    return string.Empty;
}

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. 这依赖于一个事实,即资源管理器进程是单个实例,因此您最终不会让多个资源管理器进程以不同的用户凭据运行。

1) Cassia should be able to give you a list of currently logged in users including RDC. 1) Cassia应该能够为您提供当前登录用户的列表,包括RDC。

foreach (ITerminalServicesSession sess in new TerminalServicesManager().GetSessions())
{
    // sess.SessionId
    // sess.UserName
}

2) WMI (SO answer ) 2)WMI(SO 回答

Select * from Win32_LogonSession

3) PInvoke to WTSEnumerateSessions 3) P调用WTSEnumerateSessions

4) Enumerate all instances of "explorer.exe" and get the owner using PInvoke ( OpenProcessHandle ). 4)枚举“ explorer.exe”的所有实例,并使用PInvoke( OpenProcessHandle )获取所有者。

Process[] processes = Process.GetProcessesByName("explorer");

This is a bit hacky. 这有点hacky。 WMI can also be used for this. WMI也可以用于此目的。

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. 如果您决定使用使用WMI的解决方案,则最好将winmgmt设置为服务的依赖项。

You will probably need to use win32 API for that. 您可能需要为此使用win32 API。 Read about Window Station and Desktop functions here: http://msdn.microsoft.com/en-us/library/ms687107%28v=vs.85%29.aspx 在此处阅读有关Window Station和桌面功能的信息: http : //msdn.microsoft.com/zh-cn/library/ms687107%28v=vs.85%29.aspx

Also see this question: Get the logged in Windows user name associated with a desktop 另请参阅以下问题: 获取与桌面关联的登录的Windows用户名

Maybe you could start as normal user, save user name, then programmatically request elevation : 也许您可以以普通用户身份开始,保存用户名,然后以编程方式请求提升权限:

Windows 7 and Vista UAC - Programmatically requesting elevation in C# Windows 7和Vista UAC-以编程方式在C#中请求提升

All .NET libraries will give you the user from the current context ('Administrator' in your case). 所有.NET库都会为您提供当前上下文中的用户(在您的情况下为“管理员”)。

If you are trying to secure your code, you might consider reading about: Security in the .NET framework 如果您尝试保护代码安全,则可以考虑阅读以下内容: .NET框架中的安全性

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM