获取当前登录用户(FullToken Context)

发布于 2024-10-08 04:10:13 字数 227 浏览 0 评论 0原文

我有一个问题,那就是......我用右键单击启动一个程序->以管理员身份运行。 这意味着该程序正在管理上下文中运行。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光磨忆 2024-10-15 04:10:13

您可以尝试使用 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;
}

这依赖于这样一个事实:资源管理器进程是单个实例,因此您最终不会有多个资源管理器进程使用不同的用户凭据运行的可能性。

You could try using WMI (System.Management.dll) to get the owner of the explorer.exe process.

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.

も让我眼熟你 2024-10-15 04:10:13

您可能需要为此使用 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

在风中等你 2024-10-15 04:10:13

也许您可以以普通用户身份启动,保存用户名,然后以编程方式请求提升:

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#

極樂鬼 2024-10-15 04:10:13

所有 .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

帅冕 2024-10-15 04:10:13

1) Cassia 应该能够为您提供当前登录用户的列表,包括 RDC。

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

2) WMI (SO 答案)

Select * from Win32_LogonSession

3) PInvoke到 WTSEnumerateSessions

4) 枚举所有实例“explorer.exe”并使用 PInvoke 获取所有者 (OpenProcessHandle)。

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

这有点hacky。 WMI 也可用于这。

设置 winmgmt< 可能是个好主意如果您决定使用使用 WMI 的解决方案,则将 /a> 作为服务的依赖项。

1) Cassia should be able to give you a list of currently logged in users including RDC.

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

2) WMI (SO answer)

Select * from Win32_LogonSession

3) PInvoke to WTSEnumerateSessions

4) Enumerate all instances of "explorer.exe" and get the owner using PInvoke (OpenProcessHandle).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文