如何使用 C# 在 .NET 中获取当前用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1240373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the current username in .NET using C#?
提问by Yves
How do I get the current username in .NET using C#?
如何使用 C# 在 .NET 中获取当前用户名?
采纳答案by juan
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
回答by JaredPar
Try the property: Environment.UserName
.
试试属性:Environment.UserName
。
回答by John Cuckaroo
Use:
用:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
That will be the logon name.
那将是登录名。
回答by jay_t55
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.
希望这有帮助。
回答by Israel Margulies
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.
选择您想要的格式。
回答by Kobus
The documentation for Environment.UserName seems to be a bit conflicting:
Environment.UserName 的文档似乎有点矛盾:
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 测试 Environment.UserName,它将为您提供 RunAs 用户帐户名,而不是最初登录到 Windows 的用户。
回答by Remy
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.UserName
在用户实际记录为 Environment.UserName
仍返回正在使用的当前进程的帐户。
回答by Michael B
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;
}
}
回答by Daniel E.
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.
这仅给了我实际的用户域用户名。
回答by Shivam657
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\UserName。例如,欧洲\用户名
Which is different from:
与以下不同:
String UserName = Environment.UserName;
Which displayed in the format: UserName
其中显示的格式为: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\UserName
(在本地服务器上运行应用程序时)。