简体   繁体   中英

Get The Public IP Address Of The User Who Is Visiting My Website

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)

try this...

 public string GetIpAddress()
    {
         var ipAddress=Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
         if(string.IsNullOrEmpty(ipAddress))
         {
             return Request.ServerVariables["REMOTE_ADDR"];
         }
         return ipAddress;
    }

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM