如果您指向浏览器后退按钮,我使用下面的代码(vb,但您可以转换它):
web.config:
<sessionState timeout="20" regenerateExpiredSessionId="true"/>
avsessexp.js(如果 15 分钟后不活动,则自动刷新页面):
var BeatTimer;
function StartCheck() {
if (BeatTimer == null) {
BeatTimer = setInterval("CheckBeat()", 900000);
}
}
function CheckBeat() {
PageMethods.PokePage();
}
并在每一页调用StartCheck函数(<body onload="StarCheck();">)
在我放置的每个页面的标题中:
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache, no-store, max-age=0, must-revalidate" />
<meta http-equiv="expires" content="0" />
Module1.vb
Public Sub CheckSession()
With HttpContext.Current
.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(-1))
.Response.Cache.SetCacheability(HttpCacheability.NoCache)
.Response.Cache.SetNoStore()
If .Session.IsNewSession = True Then LogOut()
'there You can put some code checking is user logged, ...
End With
End Sub
Public Sub LogOut()
With HttpContext.Current
.Session.RemoveAll()
.Session.Clear()
.Session.Abandon()
Try
.Response.Cookies.Add(New System.Web.HttpCookie("ASP.NET_SessionId", ""))
Catch ex As Exception
End Try
.Response.Redirect("Default.aspx")
End With
End Sub
并且,在每个页面中(代码隐藏):
<System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Sub PokePage()
CheckSession()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then CheckSession()
End Sub
如果您在注销后尝试单击浏览器返回按钮,CheckUser 将阻止返回上一页,并始终将用户重定向到登录页面。
在每个页面/母版页上使用非常重要<asp:ScriptManager runat="server" id="sm1" EnablePageMethods="True"><asp:ScriptManager>
附言对不起我的英语不好。