get--ip

  public string GetIp()
    {
        Regex reg1 = new Regex("(\\d+).(\\d+).(\\d+).(\\d+)");
        return reg1.Replace(GetRealIp, "*.$2.$3.$4");
    }

 

    /// <summary>
    /// 获取用户的IP地址
    /// </summary>
    public string GetRealIp
    {
        get
        {
            if (HttpContext.Current.Request.Headers["X-Forwarded-For"] != null)
            {
                return HttpContext.Current.Request.Headers["X-Forwarded-For"].Split(',')[0];//X-Forwarded-For: client1, proxy1, proxy2
            }
            if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            return HttpContext.Current.Request.UserHostAddress;
        }
    }

posted @ 2015-04-30 09:26  楊柳  阅读(419)  评论(0编辑  收藏  举报