How do I get the current username in .NET using C#?

后端 未结 18 1534
忘了有多久
忘了有多久 2020-11-22 09:59

How do I get the current username in .NET using C#?

相关标签:
18条回答
  • 2020-11-22 10:11

    For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:14

    I've tried all the previous answers and found the answer on MSDN after none of these worked for me. See 'UserName4' for the correct one for me.

    I'm after the Logged in User, as displayed by:

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

    Here's a little function I wrote to try them all. My result is in the comments after each row.

    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;
    }
    

    Calling this function returns the logged in username by return.

    Update: I would like to point out that running this code on my Local server instance shows me that Username4 returns "" (an empty string), but UserName3 and UserName5 return the logged in User. Just something to beware of.

    0 讨论(0)
  • 2020-11-22 10:16
    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
    0 讨论(0)
  • Use:

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

    That will be the logon name.

    0 讨论(0)
  • 2020-11-22 10:17

    I went over most of the answers here and none of them gave me the right user name.

    In my case I wanted to get the logged in user name, while running my app from a different user, like when shift+right click on a file and "Run as a different user".

    The answers I tried gave me the 'other' username.

    This blog post supplies a way to get the logged in user name, which works even in my scenario:
    https://smbadiwe.github.io/post/track-activities-windows-service/

    It uses Wtsapi

    0 讨论(0)
  • 2020-11-22 10:18

    If you are in a network of users, then the username will be different:

    Environment.UserName
    - Will Display format : 'Username'
    

    rather than

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

    Choose the format you want.

    0 讨论(0)
提交回复
热议问题