Using Environment.UserName returned server name, then application pool name. That is what I encountered recently. And since I needed to know who is using that small web app, as it is logged into an Oracle table, I had to find out how to get this info correctly.
Searching the web usually and always gives a lot of answers. And you have to fish out what may work. But apparently, I had to really search for longer.
My goal is to get the logged-on user from a laptop, as well as the PC name. And aside from the failure of returning the server name, the application pool name, and even the IIS IUSR at one time, that is not what I wanted. It had to be the logged-on user, and also the machine used (Environment.MachineName).
Fortunately, I managed to piece together and make do 3 points:
1. In the server, you have to disable the Web Site's Anonymous Authentication setting.
2. Then, to get the logged-on user, use Request.LogonUserIdentity.Name.
3. Finally, get PC or computer name using System.Net.Dns.GetHostEntry(Request.ServerVariables("remote_addr")).HostName
1. Disable the Web Site's Anonymous Authentication Setting
In the server where your web application is published or deployed, select and double-click on your website. In the IIS section, double-click on Authentication. Right-click on Anonymous Authentication and select Disable if it is Enabled. Step one is done.
2. Capture Local UserName
Below is the code sample, where I employed redundancy. This will work only when step 1 is done.
Dim userName As String = Environment.UserName
Dim pcUser As String = Request.LogonUserIdentity.Name
If (pcUser.Trim.Length > 0) Then
userName = pcUser
End If
3. Capture Local Computer Name
Finally, use the below code to get the local PC or computer name. Again, this is employing redundancy.
Dim pcName As String = Environment.MachineName
Dim comp_name() As String = System.Net.Dns.GetHostEntry(Request.ServerVariables("remote_addr")).HostName.Split(New Char() {"."c})
pcName = comp_name(0).ToString()
There you go! 3 steps to capture the local user and local computer name. Nothing else, or if these steps don't work for you, then you need something else. Otherwise, you are all set. Hope this helps, how to get the local or remote logged-on user.
Till then!