How do I get the current username in .NET using C#?
如何使用c#获取。net中的当前用户名?
15 个解决方案
#1
695
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
#2
249
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.
选择你想要的格式。
#4
23
The documentation for Environment.UserName seems to be a bit conflicting:
的文档环境。用户名似乎有点矛盾:
环境。用户名属性
On the same page it says:
在同一页上写着:
Gets the user name of the person who is currently logged on to the Windows operating system.
获取当前登录到Windows操作系统的人的用户名。
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.
如果你测试环境。使用RunAs的用户名,它将为您提供RunAs用户帐户名,而不是最初登录到Windows的用户名。
#5
21
I totally second the other answers, but I would like to highlight one more method which says
我完全支持其他答案,但我想再强调一个方法
String UserName = Request.LogonUserIdentity.Name;
The above method returned me the username in the format: DomainName\UserName. For example, EUROPE\UserName
上面的方法以DomainName\用户名的格式返回给我用户名。例如,欧洲\用户名
Which is different from:
这是不同的:
String UserName = Environment.UserName;
Which displayed in the format: UserName
以何种格式显示:用户名
And finally:
最后:
String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
which gave: NT AUTHORITY\IUSR
(while running the application on IIS server) and DomainName\UserName
(while running the application on a local server).
给出:NT AUTHORITY IUSR(在IIS服务器上运行应用程序)和DomainName\用户名(在本地服务器上运行应用程序)。
#6
21
Use:
使用:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
That will be the logon name.
这就是登录名。
#7
9
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.
希望这对你有帮助。
#8
9
I tried several combinations from existing answers, but they were giving me
我尝试了几种现有答案的组合,但他们给了我答案
DefaultAppPool
IIS APPPOOL
IIS APPPOOL\DefaultAppPool
I ended up using
我最终使用
string vUserName = User.Identity.Name;
Which gave me the actual users domain username only.
它只给了我实际的用户名。
#9
7
Use System.Windows.Forms.SystemInformation.UserName
for the actually logged in user as Environment.UserName
still returns the account being used by the current process.
使用System.Windows.Forms.SystemInformation。实际登录用户的用户名作为环境。用户名仍然返回当前进程使用的帐户。
#10
5
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.
我尝试了所有之前的答案,并在MSDN上找到了答案,因为这些都对我不起作用。请参见“UserName4”为我找到正确的用户名。
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.
更新:我想指出,在本地服务器实例上运行这段代码会显示Username4返回“”(一个空字符串),但是UserName3和UserName5返回登录用户。只是要小心。
#11
4
Here is the code (but not in C#):
这是代码(但不是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
Here is the code (now also in C#):
这是代码(现在也是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;
}
}
#12
3
try this
试试这个
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
now it looks better
现在它看起来更好
#13
2
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.
对于一个将要分发给几个用户的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;
}
}
}
#14
2
Just in case someone is looking for user Display Name as opposed to User Name, like me.
以防有人在寻找用户显示名而不是像我这样的用户名。
Here's the treat :
这里的治疗:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
Add Reference to System.DirectoryServices.AccountManagement
in your project.
System.DirectoryServices添加引用。AccountManagement在您的项目。
#15
1
Get the current Windows username:
获取当前的Windows用户名:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
// <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName);
}
}
#1
695
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
#2
249
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.
选择你想要的格式。
#3
#4
23
The documentation for Environment.UserName seems to be a bit conflicting:
的文档环境。用户名似乎有点矛盾:
环境。用户名属性
On the same page it says:
在同一页上写着:
Gets the user name of the person who is currently logged on to the Windows operating system.
获取当前登录到Windows操作系统的人的用户名。
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.
如果你测试环境。使用RunAs的用户名,它将为您提供RunAs用户帐户名,而不是最初登录到Windows的用户名。
#5
21
I totally second the other answers, but I would like to highlight one more method which says
我完全支持其他答案,但我想再强调一个方法
String UserName = Request.LogonUserIdentity.Name;
The above method returned me the username in the format: DomainName\UserName. For example, EUROPE\UserName
上面的方法以DomainName\用户名的格式返回给我用户名。例如,欧洲\用户名
Which is different from:
这是不同的:
String UserName = Environment.UserName;
Which displayed in the format: UserName
以何种格式显示:用户名
And finally:
最后:
String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
which gave: NT AUTHORITY\IUSR
(while running the application on IIS server) and DomainName\UserName
(while running the application on a local server).
给出:NT AUTHORITY IUSR(在IIS服务器上运行应用程序)和DomainName\用户名(在本地服务器上运行应用程序)。
#6
21
Use:
使用:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
That will be the logon name.
这就是登录名。
#7
9
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.
希望这对你有帮助。
#8
9
I tried several combinations from existing answers, but they were giving me
我尝试了几种现有答案的组合,但他们给了我答案
DefaultAppPool
IIS APPPOOL
IIS APPPOOL\DefaultAppPool
I ended up using
我最终使用
string vUserName = User.Identity.Name;
Which gave me the actual users domain username only.
它只给了我实际的用户名。
#9
7
Use System.Windows.Forms.SystemInformation.UserName
for the actually logged in user as Environment.UserName
still returns the account being used by the current process.
使用System.Windows.Forms.SystemInformation。实际登录用户的用户名作为环境。用户名仍然返回当前进程使用的帐户。
#10
5
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.
我尝试了所有之前的答案,并在MSDN上找到了答案,因为这些都对我不起作用。请参见“UserName4”为我找到正确的用户名。
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.
更新:我想指出,在本地服务器实例上运行这段代码会显示Username4返回“”(一个空字符串),但是UserName3和UserName5返回登录用户。只是要小心。
#11
4
Here is the code (but not in C#):
这是代码(但不是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
Here is the code (now also in C#):
这是代码(现在也是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;
}
}
#12
3
try this
试试这个
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
now it looks better
现在它看起来更好
#13
2
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.
对于一个将要分发给几个用户的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;
}
}
}
#14
2
Just in case someone is looking for user Display Name as opposed to User Name, like me.
以防有人在寻找用户显示名而不是像我这样的用户名。
Here's the treat :
这里的治疗:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
Add Reference to System.DirectoryServices.AccountManagement
in your project.
System.DirectoryServices添加引用。AccountManagement在您的项目。
#15
1
Get the current Windows username:
获取当前的Windows用户名:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
// <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName);
}
}