Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何捕获包含在下面代码中的WINDOWS用户名?_C#_Asp.net - Fatal编程技术网

C# 如何捕获包含在下面代码中的WINDOWS用户名?

C# 如何捕获包含在下面代码中的WINDOWS用户名?,c#,asp.net,C#,Asp.net,见 发件人: 如果使用Environment.UserName获取ASP.Net应用程序中的当前用户,则可能会获得“网络服务”,因为该用户正在运行IIS(除非其他用户正在运行IIS) 如果要获取当前用户,请执行以下操作: Console.WriteLine("UserName: {0}", System.Environment.UserName); Windows窗体应用程序: Show(System.Windows.Forms.SystemInformation.UserName) 对于A

发件人:

如果使用
Environment.UserName
获取ASP.Net应用程序中的当前用户,则可能会获得“网络服务”,因为该用户正在运行IIS(除非其他用户正在运行IIS)

如果要获取当前用户,请执行以下操作:

Console.WriteLine("UserName: {0}", System.Environment.UserName);
  • Windows窗体应用程序:

    Show(System.Windows.Forms.SystemInformation.UserName)

  • 对于Asp.net网站:

    HttpContext.Current.User.Identity.Name

其他方式

public static string CurrentUserName
{
    get
    {
        System.Security.Principal.IPrincipal _User;
        _User = System.Web.HttpContext.Current.User;
        System.Security.Principal.IIdentity _Identity;
        _Identity = _User.Identity;
        string _Value;
        _Value = _Identity.Name.Substring(_Identity.Name.IndexOf(@"\")+1);
        return _Value;
    }
}
public static string CurrentDomain
{
    get
    {
        System.Security.Principal.IPrincipal _User;
        _User = System.Web.HttpContext.Current.User;
        System.Security.Principal.IIdentity _Identity;
        _Identity = _User.Identity;
        string _Value;
        _Value = _Identity.Name.Substring(0, _Identity.Name.IndexOf(@"\"));
        return _Value;
    }
}
使用WMI(Windows Management Instrumentation):

使用制度管理

public string GetUserName()
        {
            return System.Environment.UserName;

            //Gets the name of the user who started this thread
        }
未经管理的方式:

使用


抱歉-1,但此答案在ASP.NET上下文中不正确。不希望任何来自搜索引擎的人看到这个被接受的答案而忽略下面的正确答案。
public string GetUserName()
        {
            return System.Environment.UserName;

            //Gets the name of the user who started this thread
        }
public string GetUserName()
        {

            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT UserName FROM Win32_ComputerSystem");
            string user = string.Empty;
            foreach (ManagementObject queryObj in searcher.Get())
            {
                user = Convert.ToString(queryObj["UserName"]);

            }

            return user;

        }
using System.Runtime.InteropServices;
public string GetUserName()
        {
            byte[] user = new byte[256];
            Int32[] len = new Int32[1];
            len[0] = 256;
            GetUserName(user, len);

            return (System.Text.Encoding.ASCII.GetString(user));

        }

[DllImport("Advapi32.dll", EntryPoint = "GetUserName",
        ExactSpelling = false, SetLastError = true)]
        static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] nSize);
using System.Security.Principal;
public string GetUserName()
        {                

            return( WindowsIdentity.GetCurrent().Name);


        }