myhttpmodule.cs

来自「IP锁定程序实例源码 ip focus program example reco」· CS 代码 · 共 41 行

CS
41
字号
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
//该源码下载自www.51aspx.com(51aspx.com)

namespace BlockIP
{
    public class MyHttpModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            //接收请求开始处理
            app.BeginRequest += new EventHandler(app_BeginRequest);
        }

        void app_BeginRequest(object sender, EventArgs e)
        {
            Hashtable hash = (Hashtable)HttpContext.Current.Application["blockip"];
            if (hash != null && hash.Contains(HttpContext.Current.Request.UserHostAddress))
            {
                HttpContext.Current.Response.Write("来源IP["+HttpContext.Current.Request.UserHostAddress.ToString()+"]不在允许访问之内<br><br>请与管理员联系解决!");//输出提示信息。
                HttpContext.Current.Response.End();//终止请求。
            }
            else
            { 
                HttpContext.Current.Response.Write("恭喜,你的IP不在受限访问51aspx之列!");//输出提示信息。
            
            }
        }

        public void Dispose() { }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?