Get User's IP Address

后端 未结 5 1702
半阙折子戏
半阙折子戏 2020-12-05 10:39

How can I get the current visitors IP address?

5条回答
  •  感动是毒
    2020-12-05 11:33

    public String GetIP()
    {
        string ipString;
        if (string.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
        {
            ipString = Request.ServerVariables["REMOTE_ADDR"];
        }
        else
        {
            ipString = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
        }
        return ipString;
    }
    

    First trying to find out proxy IP,if its null we can get that system IP

提交回复
热议问题