简体   繁体   English

在C# mvc中公开ip访问我网站的用户的地址和位置

[英]getting public ip address and location of users who visit my website in C# mvc

i have a website and I want to store ip address and location of the users who are visiting my website.我有一个网站,我想存储访问我网站的用户的 ip 地址和位置。 I tried many ways but the code below gives me the ip of server where my website is hosted and not client's ip.我尝试了很多方法,但下面的代码给了我托管网站的服务器 ip,而不是客户端的 ip。

//First code I tried. //我试过的第一个代码。

bool GetLan = false; 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();

//second code i tried //我试过的第二个代码

string VisitorsIPAddr = string.Empty;
        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            VisitorsIPAddr = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        else if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0)
        {
            VisitorsIPAddr = System.Web.HttpContext.Current.Request.UserHostAddress;
        }

HttpContext.Current.Request or HttpContext.Request will contain the client's IP address - you don't need the above boilerplate code. HttpContext.Current.RequestHttpContext.Request将包含客户端的 IP 地址 - 您不需要上述样板代码。

Try using the HttpRequest.UserHostAddress property in .NET Framework:尝试使用 .NET 框架中的HttpRequest.UserHostAddress属性:

public ActionResult Test()
{
    string clientIp = HttpContext.Current.Request.UserHostAddress;

    ...
}


Do note however that if there have been proxy servers between the client & your server, the IP may not be the client's IP but the proxy server's IP .但是请注意,如果客户端和您的服务器之间存在代理服务器,则 IP 可能不是客户端的 IP 而是代理服务器的 IP

In these cases, you may find the original client IP in the X-Forwarded-For header but there are no guarantees for the presence of this header nor you being aware of the client's real IP in general.在这些情况下,您可能会在X-Forwarded-For header 中找到原始客户 IP,但不能保证此 header 的存在,也不能保证您通常知道客户的真实 IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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