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

2018-12-31 21:39发布

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

标签: c# .net
17条回答
不再属于我。
2楼-- · 2018-12-31 21:50

The documentation for Environment.UserName seems to be a bit conflicting:

Environment.UserName Property

On the same page it says:

Gets the user name of the person who is currently logged on to the Windows operating system.

AND

displays the user name of the person who started the current thread

If you test Environment.UserName using RunAs, it will give you the RunAs user account name, not the user originally logged on to Windows.

查看更多
旧人旧事旧时光
3楼-- · 2018-12-31 21:51

You may also want to try using:

Environment.UserName;

Like this...:

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

Hope this has been helpful.

查看更多
浮光初槿花落
4楼-- · 2018-12-31 21:51

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.

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 21:53

Get the current Windows username:

using System;

class Sample
{
    public static void Main()
    {
        Console.WriteLine();

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserName: {0}", Environment.UserName);
    }
}
查看更多
公子世无双
6楼-- · 2018-12-31 21:54
String myUserName = Environment.UserName

This will give you output - your_user_name

查看更多
深知你不懂我心
7楼-- · 2018-12-31 21:55

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;
        }
    }
}
查看更多
登录 后发表回答