Click here to Skip to main content
14,297,280 members

Back to Basics – Get Client Computer Name in ASP.NET

Rate this:
5.00 (6 votes)
Please Sign up or sign in to vote.
5.00 (6 votes)
1 Sep 2010CPOL
How to get the client's computer name using ASP.NET

Today, I had a design review meeting about a new small application that I’m participating in its development as an architect. One of the crucial features in this application was depending on the ability to identify the client’s computer name (don’t ask me why…). Since this was a crucial thing for my customer, I decided to write about it. Pay attention that the solution I offer is for intranet application since it is counting on a DNS server.

Get Client Computer Name

In order to retrieve the client’s computer name, you will have to query the DNS server with the client’s IP. The IP can be easily retrieved by using the ASP.NET Request object with its UserHostAddress property.
Here is a method that returns the computer name by a given IP:

public string GetComputerName(string clientIP)
{                        
    try
    {                
        var hostEntry = Dns.GetHostEntry(clientIP);
        return hostEntry.HostName;
    }
    catch (Exception ex)
    {
        return string.Empty;
    }            
}

In order to use this method, you’ll have to reference the System.Net namespace which includes the Dns class. To consume the method, you can write the following code in your ASP.NET application:

GetComputerName(Request.UserHostAddress);

Summary

There are rare times that you need to know what is the name of the computer of your client. I showed how to achieve that using the System.Net.Dns class. Probably there are more solutions to this problem but this one is very easy to implement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
Questionit return ip address of user not return user machine name when i deploye it on server Pin
jjjahangir10-Sep-12 0:39
memberjjjahangir10-Sep-12 0:39 
GeneralMy vote of 5 Pin
Prajakta Bhagwat20-Nov-11 19:33
memberPrajakta Bhagwat20-Nov-11 19:33 
Questionnot working in my production server (remote server) ...? Pin
Member 33441297-Sep-10 9:01
memberMember 33441297-Sep-10 9:01 
AnswerRe: not working in my production server (remote server) ...? Pin
Gil Fink7-Sep-10 19:49
memberGil Fink7-Sep-10 19:49 
GeneralRe: not working in my production server (remote server) ...? Pin
Member 33441298-Sep-10 8:36
memberMember 33441298-Sep-10 8:36 
GeneralMy vote of 5 Pin
maq_rohit1-Sep-10 5:49
professionalmaq_rohit1-Sep-10 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 1 Sep 2010

Tagged as

Stats

61.1K views
5 bookmarked