IP address of the user who is browsing my website

后端 未结 4 1291
梦毁少年i
梦毁少年i 2021-01-07 07:59

I want to know the IP address of the client machine, i.e. the IP address of the user who is browsing my website. I am trying the following code but it is returning server ad

相关标签:
4条回答
  • 2021-01-07 08:23
    string myIP = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
    

    Note: For getting the public ip address this is used.

    0 讨论(0)
  • 2021-01-07 08:26

    Client IP can be read from request:

    context.Request.ServerVariables["REMOTE_HOST"] 
    

    Here is code for getting more than just client IP address:

    string browserInfo =
                 "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
                + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
                + "Type=" + context.Request.Browser.Type + ";\n"
                + "Name=" + context.Request.Browser.Browser + ";\n"
                + "Version=" + context.Request.Browser.Version + ";\n"
                + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
                + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
                + "Platform=" + context.Request.Browser.Platform + ";\n"
                + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
                + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
                + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
                + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";
    
    0 讨论(0)
  • 2021-01-07 08:34
    string IPAddress = string.Empty;
    string SearchName = string.Empty;
    
    String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();
    
    IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
    
    0 讨论(0)
  • 2021-01-07 08:38

    You can use "HTTP_X_FORWARDED_FOR" or "REMOTE_ADDR" header attribute.

    Refer method GetVisitorIPAddress from developersnote.com blog .

         /// <summary>
        /// method to get Client ip address
        /// </summary>
        /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param>
        /// <returns></returns>
        public static string GetVisitorIPAddress(bool GetLan = false)
        {
            string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
            if (String.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    
            if (string.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = 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";
                            }
                        }
                    }
                }
            }
    
    
            return visitorIPAddress;
        }
    
    0 讨论(0)
提交回复
热议问题