2

I need to get the server (client pc) OS username (not the machine name) locally. I found the code

Session ["OS_USER"] = Environment.UserName;

but when I host IIS Environment.UserName it captures the IIS username.

  • 1
    You can not get OS level details of client machine... why you want to do this? – Chetan Dec 23 '21 at 06:07
  • That is an educational purpose. If the OS is windows then I need to get the windows user name. – Eranda Madusanka Dec 23 '21 at 06:12
  • 1
    You can not get the windows user name of client machine using server code. You can use javascript and ActiveX. But most of the browsers do not allow ActiveX execution. If your web application runs with Windows Authentication, then you can get the logged in user details. But Windows Authentication makes sense when your application is to be used internally by users belonging to Active Directory only. For public facing web application this is not possible. – Chetan Dec 23 '21 at 06:17
  • I got it. Thank you. – Eranda Madusanka Dec 23 '21 at 06:30

1 Answers1

0

Take a look at How do I get the current username in .NET using C#?

For Windows following code might work:

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Tuor
  • 11
  • 1