How to identify who the client is, in an intranet environment

Coreysan 1,736 Reputation points
2023-10-31T23:54:01.7733333+00:00

I realize browser identity is very secure, but I wanted to know if the following can be done:

I have a C# ASP.NET app running on company intranet server.

Right now, in Code behind I can use IPHostEntry to get the client machine name, so that's good. Once I know the machine name, can I get the Windows username?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,826 Reputation points Microsoft Vendor
    2023-11-01T08:49:45.5933333+00:00

    Hi @Coreysan,

    Once I know the machine name, can I get the Windows username?

    There is no necessary connection between the two.

    You can try using

    Environment.UserName (If you test Environment.UserName using RunAs, it will give you the RunAs user account name, not the user originally logged on to Windows.)

    or

    String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name (returns DomainName\UserName);

    or

    set authentication mode to Windows in your configuration & also disable anonymous users in authorization tag.

    <configuration>
        <system.web>
            <authentication mode="Windows" />
             <authorization>
                 <deny users="?"/>
              </authorization>    
        </system.web> 
    </configuration>
    

    var userId = this.User.Identity.Name;

    Enabling Windows Authentication within an Intranet ASP.NET Web application

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. SurferOnWww 3,276 Reputation points
    2023-11-01T01:04:02.18+00:00

    Please consider use of the Windows Authentication.

    Windows Authentication

    To my knowledge, there is no way to get the Windows username from the HostName and other information obtained from the properties of IPHostEntry.

    You will be able to use the ASP.NET Identity or ASP.NET Forms Authentication instead of the Windows Authentication to get the username provided that a user registers his Windows username as the id of such authentication system.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.