判断Session是否登录,否则跳转登录页面

第一种方法:
public partial class BasePage : System.Web.UI.Page
    {
        public BasePage(
        {
            this.Load += new EventHandler(BasePage_Load);
        }
        protected void BasePage_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("~/index.html");
            }
        }
    }

其他页面只要继承这个BasePage即可!
public partial class CheckBox : BasePage
    {
        protected void Page_Load(object sender, EventArgs e){}
    }

---------------------------------------------------------------------------------------------------------------
或者采用这种方式:   原理是, 每次请求时都会调用托管代码模块
  在Web.config中配置在Configuration节点modules下添加配置访问地点
  <system.webServer>
    <modules>
      <add type="RichardTest.RequestHandle" name="demo"/>
       <!--type是指命名空间.类名-->
    </modules>
  </system.webServer>

在RequestHandle中可以这么处理
    public class RequestHandle :IHttpModule  //需要继承此接口
    {
        public void Dispose()
        { }
        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(CheckIsLog);
        }
        public void CheckIsLog(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            if (app.Context.Session != null && app.Context.Session["UserInfo"] == null && app.Context.Request.Url.ToString().IndexOf("Compare.aspx") < 0)  //当登录页时无需验证,即可成功跳转
            {
                app.Response.Redirect("http://www.baidu.com");
            }
        }
    }

这两种方法区别:(建议 使用第二种方法
  1. 使用BasePage时需要每个页面继承,写法麻烦,且有遗漏风险
  2. 使用BasePage时,当页面存在Frame时,可能会在页面某个部分跳转其他页面。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值