[问题] asp.net抓client的ip

楼主: kyo0010 (~KYO~)   2016-04-28 15:52:56
想试试抓client的ip来做一些保护的动作
但是求神了一天还是试不出来 一直抓到防火墙的ip
先附上测试code
private string GetIP()
{
string ip;
string trueIP = string.Empty;
//先取得是否有经过代理服务器
ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
//没经过代理服务器,直接使用 ServerVariables["REMOTE_ADDR"]
//并经过 CheckIP( ) 的验证
trueIP = CheckIP(Request.ServerVariables["REMOTE_ADDR"]) ?
Request.ServerVariables["REMOTE_ADDR"] : "";
}
return trueIP;
}
/// <summary>
/// 检查 IP 是否合法
/// </summary>
/// <param name="strPattern">需检测的 IP</param>
/// <returns>true:合法 false:不合法</returns>
private bool CheckIP(string strPattern)
{
// 继承自:System.Text.RegularExpressions
// regular: ^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$
Regex regex = new
Regex("^\\d{1,3}[\\.]\\d{1,3}[\\.]\\d{1,3}[\\.]\\d{1,3}$");
Match m = regex.Match(strPattern);
return m.Success;
}
抓到的都是防火墙的ip 无法抓到client的固定ip
是不是防火墙有一些设定 会转址还是其他动作
硬件小弟真的是外行 麻烦大家指引一个方向
作者: sosokill (vieux)   2016-04-28 21:18:00
要抓外网IP?喔 又是你 我还是不要发言好了 等等又被你酸

Links booklink

Contact Us: admin [ a t ] ucptt.com