2012-02-17 63 views
0

我有一个asp.net web应用程序,其中sessionstate模式是“InProc”。对于Inproc,默认情况下,会话到期时间为20分钟。我想在会话过期前的一分钟显示会话过期倒计时弹出窗口。但我无法找到一个属性,说有多少mimutes已经过去了。如何知道这是否是第19分钟。 现在我正在做如下:在asp.net中的会话计时器

if (Context.Session != null)// Check whether the session is null 
      { 
       if (Session.IsNewSession)// If the session is null, check whether the session is new 
       { 
       Response.Redirect("../SessionTimeout.aspx");//Redirect to time out page 
       } 
      } 
+2

这是一个空闲超时,所以它复位如果你的服务器代码执行。您需要在客户端上执行此操作,例如JavaScript的。 http://stackoverflow.com/questions/1470407/get-asp-net-session-last-access-time-or-time-to-timeout – StuartLC 2012-02-17 07:31:56

+0

如果您使用身份验证票证,FormsAuthenticationTicket.Expiration属性(DateTime)是有用。请参阅http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.expiration.aspx。 – 2012-02-17 07:55:40

回答

0

你可以使用一些ajax来实现这一点。 这里是一个可能的解决方案:

<script type="text/javascript"> 

    function checkAuthenticated() { 
     { 
      $.ajax({ 
       type: "POST", 
       url: "CheckAutheticated.asmx/checkAuthenticated", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: checkAuthenticatedOk, 
       error: checkAuthenticatedError 
      }); 
     } 
    } 
    function checkAuthenticatedOk() { } 
    function checkAuthenticatedError() { 
     $("#sessionExpiredCover").show(); 
    } 
    </script> 
    <style type="text/css"> 
    #sessionExpiredCover { 
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 100000; 
    display: none; 
    background-color: #fff; 
    /*opacity: .7; 
    filter:progid:DXImageTransform.Microsoft.BasicImage(opacity=.7);*/ 
    } 
    </style> 

<div id="sessionExpiredCover"> 
    <div style="background-color:#fff; margin:100px; height:200px; width:400px;"><h1>Session expired</h1> 
     <br /> 
     <asp:HyperLink NavigateUrl="~/Login.aspx" runat="server" Text="Please log in again" /> 
    </div> 
</div> 

,那么你必须开发将WebMethod您的倒计时代码:

<%@ WebService Language="C#" Class="CheckAutheticated" %> 

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment  the following line. 
[System.Web.Script.Services.ScriptService] 
public class CheckAutheticated : System.Web.Services.WebService { 

[WebMethod] 
public string checkAuthenticated() 
{ 
    //your countDownCode 
    return "authenticated"; 
} 

}