获取当前用户的ip地址(精确,继...)

 1 public string GetUserIp
2 {
3 get
4 {
5 string ip;
6 string[] temp;
7 bool isErr = false;
8 if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
9 ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
10 else
11 ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
12 if (ip.Length > 15)
13 isErr = true;
14 else
15 {
16 temp = ip.Split('.');
17 if (temp.Length == 4)
18 {
19 for (int i = 0; i < temp.Length; i++)
20 {
21 if (temp[i].Length > 3) isErr = true;
22 }
23 }
24 else
25 isErr = true;
26 }
27 if (isErr)
28 return "1.1.1.1";
29 else
30 return ip;
31 }
32 }

posted on 2011-12-01 11:52  slnt  阅读(278)  评论(0编辑  收藏  举报

导航