1

クライアント マシンの IP アドレス、つまり、私の Web サイトを閲覧しているユーザーの IP アドレスを知りたいです。次のコードを試していますが、サーバーアドレスを返しています-

public string GetClientIP()
{
    string result = string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return result;
}

正しい IP アドレスを見つけるにはどうすればよいですか?

4

4 に答える 4

5

クライアント IP はリクエストから読み取ることができます。

context.Request.ServerVariables["REMOTE_HOST"] 

以下は、クライアント IP アドレス以上のものを取得するためのコードです。

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";
于 2012-12-10T09:45:50.590 に答える
3
string IPAddress = string.Empty;
string SearchName = string.Empty;

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

IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
于 2012-12-10T09:38:58.633 に答える
1

「HTTP_X_FORWARDED_FOR」または「REMOTE_ADDR」ヘッダー属性を使用できます。

developersnote.com blogのメソッド GetVisitorIPAddress を参照してください。

     /// <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;
    }
于 2013-11-25T13:30:54.257 に答える