ASP.net利用IHttpModule做判断是否登录

1.添加一个类

    public class HttpModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public void Init(HttpApplication context)
        {
            // throw new NotImplementedException();
            context.PreRequestHandlerExecute += Context_PreRequestHandlerExecute;      
        }
        private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            // throw new NotImplementedException();
            //先判断是不是登陆页  (如果不是登录页{再来判断是否已经登录})
            //如果是登录页(需要登录)
            HttpApplication app = sender as HttpApplication;
            //获取请求连接
            string path = app.Context.Request.Url.ToString();
            //判断这个连接当中是否包含login.asp
            int n = path.ToLower().LastIndexOf("login.aspx");
            if (n==-1)  //不包含  (代表其他页面)
            {
                //判断是否已经登录
                if (app.Context.Session["usera"]==null)//没有登录
                {
                    app.Context.Response.Redirect("Login.aspx?src=" + path);
                }
            }
            else  //代表登录页
            {
            }
        }
    }

2.在Web.config写配置文件

<!--配置文件-->
  <system.webServer>
    <modules>
      <add name="aa" type="第四节登录判断.HttpModule"/>
    </modules>
  </system.webServer>

3.在登录的点击事件里写

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text== "12356")
            {
                //判断是否是跳转过来的页面
                if (Request["src"]!=null)
                {
                    string s = Request["src"].ToLower().ToString();
                    Session["usera"] = TextBox1.Text;
                    //跳回原来页面
                    Response.Redirect(s);
                }
                else
                {
                    Response.Redirect("index.aspx");
                }      
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值