0

I have searched and read almost every article related to my search about how to get client ipaddress and machine name but all the codes return only server ip and name. I've tried these codes also given below, it gives my PC ip on localhost but on the server, it gives server ip and name :

string stringHostName = Dns.GetHostName();                
 IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);

and also used this method :

public string GetUserIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDER_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }enter code here
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }

It gives ::1 on localhost but on the server it gives server ip only. Kindly help me if there any way possible to get client ipaddress and machine name using C#.

  • 1
    Of course it does. Server side code runs on the server. See this post instead. https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript-only – danish Apr 26 '18 at 05:15
  • @danish sir the link you put is using javascript only and also third party website to fetch the ipaddress, Which I don't need. Please tell me if it is possible using C# and Asp.net?? – Ashutosh Singh Apr 26 '18 at 05:23
  • 1
    You can't get the device name. – ProgrammingLlama Apr 26 '18 at 05:23
  • For your other question, see [here](https://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc) – ProgrammingLlama Apr 26 '18 at 05:25
  • 2
    Possible duplicate of [How can I get the client's IP address in ASP.NET MVC?](https://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc) – Nick.Mc Apr 26 '18 at 05:34
  • You can't get the machine name over the internet, for further explanation see [here](https://stackoverflow.com/a/6161635/346453) – Hogaf Apr 26 '18 at 15:41
  • okay, i got it that i can't get the machine name but pls tell me a possible way to get visiter ipaddress. I have read all the links you have put here guys but still i get server ip. On localhost i get my ipaddress by < Dns.GetHostEntry(GetHostName()) > this method but not on server.(I am not making the web app using ASP.net MVC but only ASP.NET and C#) – Ashutosh Singh Apr 27 '18 at 07:50
  • I suggest you try the link I posted. https://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc – Nick.Mc Apr 29 '18 at 10:04
  • Thank you all for helping me to solve this question. I have got ipaddress of the client but my second question is : Can we get the client machine name also by using the ipaddress of him? As Mr. john has said above that we can not get the machine name. – Ashutosh Singh May 01 '18 at 04:45

0 Answers0