web项目中登陆超时的功能实现(基于C#)

  当我们登陆进网站后,中途去看别的东西,没有再与该网站的服务器交互,就会弹出一个js窗口,登陆超时请重新登陆,并跳转到登陆页面。

  步骤1.实现原理,在web.config中配置session的超时时间,若规定时间没有再请求服务器,session就会失效。

  web.config代码如下:

 <system.web>
    <sessionState mode="StateServer" stateNetworkTimeout="20" stateConnectionString="tcpip=127.0.0.1:42424"/>

  步骤2.判断session中Login_Id是否有效,如无效,跳转到PageSession.aspx处理  

  服务器端页面刷新的代码,如下

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Login_Id"] != null)
        {
          //功能模块代码,省
        }
        else
        {
            Response.Redirect("~/PageSession.aspx");
        }
    }

   步骤3.输出js代码,输出警告框超时操作,并定向到登陆页面index_popup.aspx

   PageSession.aspx页面代码,如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Write("<script language='JavaScript'>alert('操作超时,请重新登陆!');top.location.href='index_popup.aspx';</script>");
    }

 

posted @ 2016-03-17 17:19  rongyux  阅读(1946)  评论(0编辑  收藏  举报