DCSIMG
Back to Basics – Get Client Computer Name in ASP.NET - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2011 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

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

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

Today I had a design review meeting about a new small application.Net Logo with ASP.NET
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’ve 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 the reference the System.Net namespace which include 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.

Posted: Sep 01 2010, 03:57 PM by Gil Fink | with no comments |
תגים:,

Comments

No Comments