在.net中查找Windows用户

在.net中查找Windows用户,.net,.net,我正在使用Environment.UserName获取.net中的用户名,但我获取的是计算机名而不是用户名。我还试着使用 System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); 甚至这个返回的机器名而不是用户名。 有什么想法吗?试试这个(在上找到) 您在哪里调用它,Divya-在客户端上,还是从客户端连接到的服务器应用程序中调用?代码中是否有其他内容以某种方式破坏了返回的字符串?您确定它没有返回计算机名和用户

我正在使用Environment.UserName获取.net中的用户名,但我获取的是计算机名而不是用户名。我还试着使用 System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); 甚至这个返回的机器名而不是用户名。 有什么想法吗?

试试这个(在上找到)


您在哪里调用它,Divya-在客户端上,还是从客户端连接到的服务器应用程序中调用?代码中是否有其他内容以某种方式破坏了返回的字符串?您确定它没有返回计算机名和用户名吗?如果这是asp.net服务器端代码,则您可能在匿名模式下看到IIS正在使用的名称。感谢您的回复:)我知道了。事实上我正试图在Dotnetnuke中获取用户名。DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().Username;解决了我的问题。再次感谢。
Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

   Public Function GetUserName() As String
       Dim iReturn As Integer
       Dim userName As String
       userName = New String(CChar(" "), 50)
       iReturn = GetUserName(userName, 50)
       GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
   End Function