2012-07-17 172 views
1

当会话超时时,这是将用户注销的正确方式吗? 这是我在主页的Page_init中编写的代码。 而且,
会话状态和窗体身份验证是喜欢,当会话超时时注销用户

<sessionState mode="InProc" timeout="1" cookieless="false"> 
    </sessionState> 

     <authentication mode="Forms"> 
      <forms loginUrl="Default.aspx" timeout="2880"/> 
     </authentication> 


    if (userToLogin != null) 
       { 
        if (Context.Session != null) 
        { 
         if (Session.IsNewSession) 
         { 
          HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"]; 
          if (newSessionIdCookie != null) 
          { 
           string newSessionIdCookieValue = newSessionIdCookie.Value; 

           if (newSessionIdCookieValue != string.Empty) 
           { 
            **System.Web.Security.FormsAuthentication.SignOut();**         
            Response.Redirect("SessionTimeOutPage.htm"); 
           } 
          } 
         } 
        } 

       } 

请建议我来说这是正确的方式或东西是可以作为最佳的解决方案。

+0

对不起,也许需要更多的测试。我用这一点来做这样的测试。 – Aristos 2012-07-17 10:38:35

回答

1

把你的代码的Global.asax.cs

protected void Session_End(Object sender, EventArgs e) 
{    

    HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authenticationCookie != null) 
    { 
    FormsAuthenticationTicket authenticationTicket = FormsAuthentication.Decrypt(authenticationCookie.Value); 
    if (!authenticationTicket.Expired) 
    { 
     // log-out the user...     
    } 
    } 

} 
+0

这将工作,但我想在会话超时后重定向到另一个页面。和Session_End给出的错误response.redirect() – 2012-07-17 10:47:19

+0

你不能在这个处理程序中做到这一点..首先没有要求重定向。你可以写一些解决方法,虽然..(http://www.codeproject.com/Articles/15575/Detecting-Session-Timeout-and-Redirect-to-HomePage)就是一个例子。 – 2012-07-17 10:59:30