How to get a client's IP Address

I have a web app that need to get the client's IP Address. I have attempted several different methods without success. I have pasted the code below.

strDNS = Dns.GetHostName();

Session["UserIP"] = Dns.GetHostEntry(strDNS).AddressList[0].ToString();

I believe this will retreive the Server's IP address.

strDNS = HttpWorkerRequest.GetRemoteAddress.ToString();

I believe something along these lines should work but VS 2005 erros on build saying "Cannot convert method group 'GetRemoteAddress' to non-delegatetype 'object'.' How can I accomplish this task



Answer this question

How to get a client's IP Address

  • TJB77494

    Sorry, posted the wrong code. Don't know what happend but here is the code i want to post:


    IPAddress[] addresses = Dns.GetHostByName( "MACHINE1" ).AddressList;

    foreach( IPAddress address in addresses )
    {
    Console.WriteLine( address.ToString() );
    }




  • AmolK

    That is great, I thing this gets the same infromation the code I posted gets. Doesn't the 'GetHostName' getting the server name and the IPAddress of the Server

    Shouldn't I be using 'HttpWorkerRequest.GetRemoteAddress' to get the client IPAddress and How would I do that


  • nukolai

    If the client has a private IP... the web server must be inside the client's network to retrieve the private IP. If you're not in the network, you'll only get the visible public IP of the router that's actually connecting to the server. The client would have to send it's internal IP in order for you to get it.
  • erb54238

    Sorry, I have been out of the office. I have attempted to change the code but I continue to get the error on build "Error 1 An object reference is required for the nonstatic field, method, or property 'System.Web.HttpWorkerRequest.GetRemoteAddress()'"

    WHat does this mean and How can i fix it


  • WestAustinite


    strDNS = Dns.GetHostName();
    Session["UserIP"] = Dns.GetHostEntry(strDNS).AddressList[0].ToString();

    strDNS = HttpWorkerRequest.GetRemoteAddress().ToString();




  • JonTec7

    If it's a web app running from asp.net, wouldn't "Context.Request.UserHostAddress" get the IP of the client connecting to it Perhaps I'm misinterpreting what the question was


  • chrisewers

    I believe you understand correctly, It is a web app that I am trying to get the User's IP Address. The IP returned from the code you provided returned, I Believe, the Router's IP Address. Is there an effective means to collect the user's IP
  • HastaVista

    Thanks...I believe that will work. I get the Ip back of 127.0.0.1....me!
  • How to get a client's IP Address