none
How to get a client's IP Address

    Question

  • 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? 

     

    Thursday, March 23, 2006 9:20 PM

Answers


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

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

     

    Friday, March 24, 2006 9:24 AM
    Moderator
  • 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?

    Tuesday, March 28, 2006 3:48 PM

All replies


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

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

     

    Friday, March 24, 2006 9:24 AM
    Moderator
  • 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?

    Tuesday, March 28, 2006 1:10 PM
  • 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() );
    }

     

    Tuesday, March 28, 2006 2:25 PM
    Moderator
  • 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?

    Tuesday, March 28, 2006 3:19 PM
  • 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?

    Tuesday, March 28, 2006 3:48 PM
  • 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?
    Tuesday, March 28, 2006 4:48 PM
  • 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.
    Tuesday, March 28, 2006 5:16 PM
  • Thanks...I believe that will work. I get the Ip back of 127.0.0.1....me!
    Tuesday, March 28, 2006 5:34 PM