作者kyo0010 (~KYO~)
看板C_Sharp
标题[问题] asp.net抓client的ip
时间Thu Apr 28 15:52:56 2016
想试试抓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
是不是防火墙有一些设定 会转址还是其他动作
硬体小弟真的是外行 麻烦大家指引一个方向
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.120.119.244
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1461829979.A.D2C.html
1F:→ sosokill: 要抓外网IP? 04/28 21:18
2F:→ sosokill: 喔 又是你 我还是不要发言好了 等等又被你酸 04/28 21:31