如何使用C#在.NET中获取当前用户名?
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
试试酒店:Environment.UserName
。
或'string userName = Environment.UserName;' – Donut 2009-08-07 18:02:14
注意:像Simon Gillbee在接受的答案的评论中提到的那样,'Environment.UsernName'给出登录的帐户名,但是'WindowsIdentity.GetCurrent()。Name'正在返回应用程序运行时的帐户名。 – Gerwald 2014-10-14 16:00:29
@leo:警告:该信息也显然不正确,请参见[Bolu的回复](http://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using -c#comment34794502_1240379)。 :-) – 2015-11-05 16:56:33
用途:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
这将是登录名。
如果线程正在提升模式下运行,则不适用。 – maxp 2015-05-06 10:55:55
您还可以尝试使用尝试:
Environment.UserName;
就像这个...:
string j = "Your WindowsXP Account Name is: " + Environment.UserName;
希望这是有帮助的。
如果用户的网络中,则用户名会有所不同:
Environment.UserName
- Will Display format : 'Username'
而不是
System.Security.Principal.WindowsIdentity.GetCurrent().Name
- Will Display format : 'NetworkName\Username'
选择您想要的格式。
您可以使用'Environment.UserDomainName +'\\“+ Environment.UserName'来获得与'System.Security.Principal.WindowsIdentity.GetCurrent()。Name'相同的结果。现在,有什么区别,你问...我不确定。 – Gutblender 2014-10-06 16:06:02
我需要让用户运行应用程序而不是谁登录(Environment.UserName不是我想要的),所以我做了以下操作来剥离域:'System.Security.Principal.WindowsIdentity.GetCurrent( ).Name.Split('\\').Last();' – thehelix 2015-01-21 20:54:44
在调用Last()之前,您需要在'Split'之后添加'ToArray()' – codenamezero 2017-11-17 16:28:17
为Environment.UserName文档似乎有点矛盾的:
在同一页上,它说:
获取谁是人的用户名当前登录到Windows操作系统。
和
显示谁开始的当前线程
如果您使用的RunAs测试Environment.UserName,它会给你的RunAs用户帐户名的人的用户名,而不是最初登录到Windows的用户。
对于实际登录的用户使用System.Windows.Forms.SystemInformation.UserName
作为Environment.UserName
仍然返回当前进程正在使用的帐户。
感谢您的分享,另请参阅:System.Windows .Forms.SystemInformation.UserDomainName – 2017-02-16 13:49:24
下面是代码(而不是在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;
}
}
我试图从现有的答案几种组合,但他们是给我
DefaultAppPool
IIS APPPOOL
IIS APPPOOL\DefaultAppPool
我结束了使用
string vUserName = User.Identity.Name;
哪给了我实际的用户域用户名。
非常好。我也只收到了应用程序池标识。但是你的代码解决了我的问题。非常感谢。 – SuryaKavitha 2014-10-30 11:47:19
这听起来像你想提出请求的用户的用户名,我想到一个MVC或Web API控制器。这与在 – 2016-03-30 08:30:33
下运行进程的用户名不同@BenAaronson这个问题没有说明什么用户名正在运行一个进程。我需要当前登录的域名用户名和搜索带我到这个页面,这段代码最终给了我我需要的。 – 2016-04-28 12:53:57
我完全第二个其他的答案,但我想强调一个更该说
String UserName = Request.LogonUserIdentity.Name;
上述方法返回我的用户名格式的方法:域名\用户名。例如,欧洲\用户名
这是从不同:
String UserName = Environment.UserName;
的格式显示:用户名
最后:
String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
这给:NT AUTHORITY\IUSR
(同时在IIS服务器上运行应用程序)和DomainName\UserName
(同时在本地运行应用程序erver)。
获取当前Windows用户名:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
// <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName);
}
}
我的网站正在使用IIS,但是当我从网络中的另一台计算机访问网页时,我一直看到“网络服务”作为用户名。我尝试了所有不同的选项,但它没有给我登录的用户查看页面。任何想法? – Si8 2016-08-08 16:53:36
我已经尝试了所有以前的答案,这些都不为我工作后找到了答案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返回登录用户。只是要提防。
我的网站使用的是IIS,但是当我从网络中的另一台计算机访问网页时,我一直看到“网络服务”作为用户名。我尝试了所有不同的选项,但它没有给我登录的用户查看页面。任何想法? – Si8 2016-08-08 16:53:27
注意:没有说清楚,但上面的示例适用于在IIS中运行的Web应用程序的上下文中,其中几个应用程序将显示执行ASP.Net应用程序池的服务器Windows帐户的名称。如果作为交互式程序或Windows服务运行,HttpContext中的一个将不可用,其他人应该用于查找该进程执行的帐户。 – 2017-06-06 13:54:26
对于要分发给几个用户的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;
}
}
}
试试这个
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
现在看起来更好
万一有人找用户显示名称而不是用户名,像我。
这里的治疗:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
添加参考System.DirectoryServices.AccountManagement
在您的项目。
这与“Environment.UserName”有什么不同? – 2009-08-06 17:44:23
这不适用于Windows 8,任何想法如何? – 2013-03-05 13:16:01
这个问题已经在这个环节中讨论过了......可能会重复提问。 http://stackoverflow.com/questions/5218778/how-to-get-currently-logged-username-from-windows-service-in-net – 2013-05-20 07:17:49