如何获得调用“以管理员身份运行"的用户的用户名? [英] How to obtain the username of user that invoked "run as administrator"?

查看:110
本文介绍了如何获得调用“以管理员身份运行"的用户的用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

有很多方法可以获取当前用户的用户名.我感兴趣的是,当用户使用以管理员身份运行"调用程序时,如何获取运行程序的用户名?

干杯,
--Alex

Hi there,

There are numerous ways to obtain the username of the current user. What I am interested in is, how can I obtain the username of the user that runs my program when the user invokes it with "run as administrator"?

Cheers,
--Alex

推荐答案

下面是将提供以管理员身份运行我的应用程序的进程的用户名的类.

它很慢,但是都是托管代码.注释中包含指向各种作品的源的链接.

干杯,
-亚历克斯


用法:
Below is the class that will provide the username of the process that runs my application as an administrator.

It''s slow, but it''s all managed code. Links to the souces of various pieces are included in the comments.

Cheers,
--Alex


Usage:
int currentProcessID = Process.GetCurrentProcess().Id;
int parentID = ParentProcess.GetID(currentProcessID);
string parentOwner = ParentProcess.GetOwner(parentID);



代码:



Code:

using System;
using System.Collections.Generic;
using System.Text;
//
using System.Diagnostics;
using System.Management;

public static class ParentProcess
{
    public static int GetID(int processID)
    {
        return Process.GetProcessById(processID).Parent().Id;
    }

    public static string GetOwner(int processId)
    //From: http://stackoverflow.com/questions/777548/how-do-i-determine-the-owner-of-a-process-in-c
    {

        string query = "Select * From Win32_Process Where ProcessID = " + processId;
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
        ManagementObjectCollection processList = searcher.Get();

        foreach (ManagementObject obj in processList)
        {
            string[] argList = new string[] { string.Empty };
            int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
            if (returnVal == 0)
            {
                return argList[0];
            }
        }

        return "NO OWNER";
    }
}

//from: http://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way
public static class ProcessExtensions
{
    private static string FindIndexedProcessName(int pid)
    {
        var processName = Process.GetProcessById(pid).ProcessName;
        var processesByName = Process.GetProcessesByName(processName);
        string processIndexdName = null;

        for (var index = 0; index < processesByName.Length; index++)
        {
            processIndexdName = index == 0 ? processName : processName + "#" + index;
            var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
            if (processId.NextValue().Equals(pid))
            {
                return processIndexdName;
            }
        }

        return processIndexdName;
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
        return Process.GetProcessById((int)parentId.NextValue());
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}
//from: http://yacsharpblog.blogspot.com/
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(
        AttributeTargets.Assembly
        | AttributeTargets.Class
        | AttributeTargets.Method,
        Inherited = false,
        AllowMultiple = false)
    ]
    public class ExtensionAttribute : Attribute
    {
    }
}


我希望如果用户使用与当前Windows用户不同的名称运行应用程序,则该应用程序应为此正确报告正确的SecurityPrincipal对象.实例.

第一个链接无效?
Edit2:第二个链接不起作用? MSDN复制和粘贴. :-/

在MSDN上搜索My.User.Name. (我厌倦了对链接的幻想.....

VB:
I would have expected that if an user is running an application under a different name from the current windows user, then the application should correctly report the correct SecurityPrincipal object for that instance.

1st link didn''t work??
2nd link didn''t work?? MSDN copy and paste is fkd up. :-/

Go search for My.User.Name on MSDN. (i''m fed up fannying about with the links.....

VB:
Function GetUserName() As String
    If TypeOf My.User.CurrentPrincipal Is _
    Security.Principal.WindowsPrincipal Then
        ' The application is using Windows authentication.
        ' The name format is DOMAIN\USERNAME.
        Dim parts() As String = Split(My.User.Name, "\")
        Dim username As String = parts(1)
        Return username
    Else
        ' The application is using custom authentication.
        Return My.User.Name
    End If
End Function


我认为您应该使用

I think you should use

System.Security.Principal.WindowsIdentity.GetCurrent.Name




or

Environment.UserName


这篇关于如何获得调用“以管理员身份运行"的用户的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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