1

I tried this below code. But it returned me ::1

string IPAddress = string.Empty;
string SearchName = string.Empty;

            String strHostName = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();

            IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

I expect output should be Something like : (358.89.48.188)

So I'm looking forward for anyone who could help me out.

(By getting this I will get the location and pass it to Session,So my Controller and action reacts based on the client location)

Gokul
  • 68
  • 1
  • 2
  • 13
  • 5
    ::1 stands for localhost. So if you try to test it locally. You will always get this. – SehaxX Jun 11 '18 at 12:20
  • @SehaxX so if i deploy and try in site can i expect it to work fine – Gokul Jun 11 '18 at 12:20
  • @gokul: What happens when you access this from a different (non-localhost) system? – David Jun 11 '18 at 12:21
  • what is value of strHostName? It should contain IP in string format. – Saurabh Harwande Jun 11 '18 at 12:25
  • Possible duplicate of [What is IP address '::1'?](https://stackoverflow.com/questions/4611418/what-is-ip-address-1) – mjwills Jun 11 '18 at 12:28
  • @David when i access from non local host the IP Address is returning the Public Ip of My hosted server,But i need the user's accessing my site – Gokul Jun 11 '18 at 12:30
  • @gokul Try this HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] instead of Request.UserHostAddress which returns server IP which IIS runs on. – SehaxX Jun 11 '18 at 12:35
  • @SehaxX Will let u know after trying this – Gokul Jun 11 '18 at 12:36
  • @SaurabhHarwande i just copied this code from this link https://stackoverflow.com/questions/13798286/ip-address-of-the-user-who-is-browsing-my-website and below is my code where I am able to get the Public Ip address of my system ,not the client System string externalIP; externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/"); externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) .Matches(externalIP)[0].ToString(); – Gokul Jun 11 '18 at 12:37
  • @SehaxX it didnt worked – Gokul Jun 11 '18 at 12:44
  • The problem is with the last line. You already have got the address inside String strHostName. You just need to IPAddress.Parse(strHostName) instead of Dns resolving. – Saurabh Harwande Jun 11 '18 at 12:57
  • @SaurabhHarwande Returning my server address(Mumbai IP) but I am accessing from TamilNadu – Gokul Jun 11 '18 at 13:23

2 Answers2

3

try this...

 public string GetIpAddress()
    {
         var ipAddress=Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
         if(string.IsNullOrEmpty(ipAddress))
         {
             return Request.ServerVariables["REMOTE_ADDR"];
         }
         return ipAddress;
    }
  • execute it on server( non- local) system. – Prateek Deshmukh Jun 11 '18 at 12:39
  • External IP166.91.48.188 Loaded PlaceOK;;89.129.8.172;IN;India;Maharashtra;Mumbai;400099;19.0144;72.8479;+05:30 ,this is what i got in my logger but i need the users(customer accessing my site's Public Ip) this ip is my public Ip address – Gokul Jun 11 '18 at 12:54
  • @gokul - one question for more clarification- for example if user is accessing your website (abc.com) using IP address xxx.xxx.xxx.xxx (pointing locally abc.com to xxx in host file), where user's public IP is yyy.yyy.yyy.yyy, so do you want xxx or yyy? – Prateek Deshmukh Jun 11 '18 at 13:09
  • My site is hosted in Mumbai Based Ip and if my Customer access it from Tamilnadu , I need the Ip Address Of his system so while converting its Ip address i will get a place from Tamilnadu – Gokul Jun 11 '18 at 13:31
  • @gokul - Request.ServerVariables["REMOTE_ADDR"], should give you yyy part. So do you think 89.129.8.172 in your log is not user's address? – Prateek Deshmukh Jun 11 '18 at 13:40
  • actually for privacy reason , i changed my return ip as 89.129.8.172 ,btw i checked the ip address from this site http://api.ipinfodb.com/v3/ip-city/?key=xxxxxxxxxxxxxxxxxxxxxx – Gokul Jun 11 '18 at 14:04
2

Heading ##Hey All Thanks For Your Contribution ,I Got Answer By Using The Following Code,Hope It Might Help Some One In Future ## Heading ##

    public string GetVisitorIPAddress(bool GetLan = false)
    {
        string visitorIPAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                   if (String.IsNullOrEmpty(visitorIPAddress))
            visitorIPAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                  if (string.IsNullOrEmpty(visitorIPAddress))
            visitorIPAddress = System.Web.HttpContext.Current.Request.UserHostAddress;

        if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
        {
            GetLan = true;
            visitorIPAddress = string.Empty;
        }
        if (GetLan)
        {
            if (string.IsNullOrEmpty(visitorIPAddress))
            {
                //This is for Local(LAN) Connected ID Address
                string stringHostName = Dns.GetHostName();
                //Get Ip Host Entry
                IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
                //Get Ip Address From The Ip Host Entry Address List
                IPAddress[] arrIpAddress = ipHostEntries.AddressList;
                try
                {
                    visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
                }
                catch
                {
                    try
                    {
                        visitorIPAddress = arrIpAddress[0].ToString();
                    }
                    catch
                    {
                        try
                        {
                            arrIpAddress = Dns.GetHostAddresses(stringHostName);
                            visitorIPAddress = arrIpAddress[0].ToString();
                        }
                        catch
                        {
                            visitorIPAddress = "127.0.0.1";
                        }
                    }
                }

            }

        }

        var zaz = "";
        zaz = visitorIPAddress.ToString();
        getcityname(zaz);
        return null;
    }
Gokul
  • 68
  • 1
  • 2
  • 13