首页 文章

如何使用C#获取.NET中的当前用户名?

提问于
浏览
492

如何使用C#获取.NET中的当前用户名?

17 回答

  • 2

    您可能还想尝试使用:

    Environment.UserName;
    

    像这样...:

    string j = "Your WindowsXP Account Name is: " + Environment.UserName;
    

    希望这有帮助 .

  • 7

    试试这个房产:Environment.UserName .

  • 4

    使用:

    System.Security.Principal.WindowsIdentity.GetCurrent().Name
    

    那将是登录名 .

  • 105

    我尝试了现有答案中的几种组合,但是他们给了我

    DefaultAppPool
    IIS APPPOOL
    IIS APPPOOL\DefaultAppPool
    

    我最终使用了

    string vUserName = User.Identity.Name;
    

    这给了我实际用户域名用户名 .

  • 10

    试试这个

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
    ManagementObjectCollection collection = searcher.Get();
    string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
    

    现在它看起来更好

  • 4

    如果您在用户网络中,则用户名将不同:

    Environment.UserName
    - Will Display format : 'Username'
    

    而不是

    System.Security.Principal.WindowsIdentity.GetCurrent().Name
    - Will Display format : 'NetworkName\Username'
    

    选择所需的格式 .

  • 741

    如果它对其他人有帮助,当我将应用程序从c#.net 3.5应用程序升级到Visual Studio 2017时,这行代码 User.Identity.Name.Substring(4); 抛出此错误“startIndex不能大于字符串的长度”(它之前没有阻止) .

    当我将其更改为 System.Security.Principal.WindowsIdentity.GetCurrent().Name 时,我很高兴但是我最终使用 Environment.UserName; 来获取登录的Windows用户而没有域部分 .

  • 5
    String myUserName = Environment.UserName
    

    这会给你输出 - your_user_name

  • 9

    我已经尝试了所有以前的答案,并在MSDN上找到答案后,这些都不适用于我 . 请参阅'UserName4'以获取正确的信息 .

    我在登录用户之后,如下所示:

    <asp:LoginName ID="LoginName1" runat="server" />
    

    这是我写的一个小函数来尝试它们 . 我的结果是在每行之后的注释中 .

    protected string GetLoggedInUsername()
    {
        string UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Gives NT AUTHORITY\SYSTEM
        String UserName2 = Request.LogonUserIdentity.Name; // Gives NT AUTHORITY\SYSTEM
        String UserName3 = Environment.UserName; // Gives SYSTEM
        string UserName4 = HttpContext.Current.User.Identity.Name; // Gives actual user logged on (as seen in <ASP:Login />)
        string UserName5 = System.Windows.Forms.SystemInformation.UserName; // Gives SYSTEM
        return UserName4;
    }
    

    调用此函数将返回登录的用户名 .

    更新:我想指出在我的本地服务器实例上运行此代码向我显示Username4返回“”(空字符串),但UserName3和UserName5返回登录用户 . 只是需要注意的事情 .

  • 0
    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
  • 273

    这是代码(但不是在C#中):

    Private m_CurUser As String
    
    Public ReadOnly Property CurrentUser As String
        Get
            If String.IsNullOrEmpty(m_CurUser) Then
                Dim who As System.Security.Principal.IIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
    
                If who Is Nothing Then
                    m_CurUser = Environment.UserDomainName & "\" & Environment.UserName
                Else
                    m_CurUser = who.Name
                End If
            End If
            Return m_CurUser
        End Get
    End Property
    

    这是代码(现在也在C#中):

    private string m_CurUser;
    
    public string CurrentUser
    {
        get
        {
            if(string.IsNullOrEmpty(m_CurUser))
            {
                var who = System.Security.Principal.WindowsIdentity.GetCurrent();
                if (who == null)
                    m_CurUser = System.Environment.UserDomainName + @"\" + System.Environment.UserName;
                else
                    m_CurUser = who.Name;
            }
            return m_CurUser;
        }
    }
    
  • 24

    以防有人正在寻找用户 Display Name 而不是 User Name ,就像我一样 .

    这是一种享受:

    System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName

    在项目中添加对 System.DirectoryServices.AccountManagement 的引用 .

  • 21

    对于要分发给多个用户的Windows窗体应用程序,其中许多用户通过vpn登录,我尝试了几种方法,这些方法都适用于本地机器测试,但不适用于其他用户 . 我遇到了一篇微软的文章,我改编并开始工作 .

    using System;
    using System.Security.Principal;
    
    namespace ManageExclusion
    {
        public static class UserIdentity
    
        {
            // concept borrowed from 
            // https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity(v=vs.110).aspx
    
            public static string GetUser()
            {
                IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
                WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
                return windowsIdentity.Name;
            }
        }
    }
    
  • 20

    我完全赞同其他答案,但我想强调另一种方法

    String UserName = Request.LogonUserIdentity.Name;
    

    上面的方法以格式返回了用户名: DomainName\UserName . 例如,EUROPE \ UserName

    这与以下内容不同:

    String UserName = Environment.UserName;
    

    其中显示格式: UserName

    最后:

    String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    

    它给出了: NT AUTHORITY\IUSR (在IIS服务器上运行应用程序时)和 DomainName\UserName (在本地服务器上运行应用程序时) .

  • 1

    对实际登录的用户使用 System.Windows.Forms.SystemInformation.UserName ,因为 Environment.UserName 仍然返回当前进程正在使用的帐户 .

  • 3

    获取当前的Windows用户名:

    using System;
    
    class Sample
    {
        public static void Main()
        {
            Console.WriteLine();
    
            //  <-- Keep this information secure! -->
            Console.WriteLine("UserName: {0}", Environment.UserName);
        }
    }
    
  • 0

    Environment.UserName的文档似乎有点冲突:

    Environment.UserName Property

    它在同一页面上说:

    获取当前登录到Windows操作系统的人员的用户名 .

    显示启动当前线程的人员的用户名

    如果使用RunAs测试Environment.UserName,它将为您提供RunAs用户帐户名,而不是最初登录到Windows的用户 .

相关问题