对用户IP进行访问限制

 1 //ashx文件 
 2 
 3 public void ProcessRequest (HttpContext context) {
 4 
 5                      string action=context.Request["action"];
 6 
 7 if (action == "GetIP")
 8 {
 9 string result;
10 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
11 if (null == result || result == String.Empty)
12 {
13 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
14 }
15 
16 if (null == result || result == String.Empty)
17 {
18 result = HttpContext.Current.Request.UserHostAddress;
19 }
20 IP ip = IPManager.GetOne("select * from IP where '" + result + "' between IP_Start and IP_End");
21 if (ip.ID > 0)
22 {
23 context.Response.Write(1);
24 }
25 else
26 {
27 context.Response.Write(0);
28 } //取得数据后返回给客户端。
29 
30 }
31 }
32 //html
33 
34 <script type="text/javascript">
35 $(function getIP() {
36 $.post("/ashx/PageNews.ashx", { "action": "GetIP" }, function (data) {
37 var json = eval(data);
38 if (json == "1") {
39 document.getElementById("div1").style.display = "";
40 }
41 else {
42 alert("您的IP地址拒绝访问网站!");
43 window.location.href = "http://www.baidu.com";
44 }
45 });
46 });
47 </script>
备注:html文件页面div1嵌套
原文地址:https://www.cnblogs.com/wdd812674802/p/9485470.html