【问题标题】:Session Timer in asp.netasp.net 中的会话计时器
【发布时间】:2012-03-08 15:06:14
【问题描述】:
我有一个会话状态模式为“InProc”的 asp.net Web 应用程序。对于 Inproc,默认情况下会话到期时间为 20 分钟。我想在会话到期前一分钟显示会话到期倒计时弹出窗口。但我找不到一个属性,说有多少模仿已经过去了。如何知道是否是第 19 分钟。
现在我正在做如下:
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
}
}
【问题讨论】:
标签:
asp.net
session
mode
inproc
【解决方案1】:
您可以使用一些 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";
}
}