像这样的东西怎么样......注意:我有几个 MVC Razor 片段可以与其他任何东西一起切换。我还没有测试过,但这是一个好的开始。非 Safari iOS 浏览器将忽略 pageshow/pagehide 事件处理程序,因此这不是移动设备的完整解决方案。请随时对此进行改进,以获得更广泛的跨浏览器功能。此外,此解决方案需要使用 JQuery cookie 插件: https ://github.com/carhartl/jquery-cookie
///////// 客户端代码
//Safari iOS example that could be further developed to handle other mobile browsers
var sessTimeCutOffInMs = 600000; //600000 ms equals 10 minutes
//Safari iOS event handler for resume tab and/or focus from sleep mode
window.addEventListener("pageshow", function(e){
var timeIn = getTime();
var timeOut = $.cookie('timeOut');
if(timeOut != null) {
//Let us compare
compareTimes(timeIn,timeOut);
}
}, false);
//Safari iOS event handler when creating new tab/app switching and/or putting into sleep mode
window.addEventListener("pagehide", function(e){
var timeOut = getTime();
$.cookie('timeOut', timeOut, { path: '/' });
}, false);
function getTime() {
@{
//MVC Razor syntax
//formatted as milliseconds since 01.01.1970
var _serverTime = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString("F0");
}
var serverTime = @_serverTime;
return serverTime;
//return (new Date()).getTime();
}
function compareTimes(timeIn,timeOut) {
var diff = timeIn - timeOut;
//If the mobile page was asleep for 10 minutes or more, force iOS user to logout
//Especially useful for when forms auth is set to slidingExpiration=true
if(diff >= sessTimeCutOffInMs) {
//Redirect to logout routine
//MVC Razor code shown below for redirecting to my Home Controller/Action
simpleReset('@(Html.ResolveUrl("~/Home/SignOut"))');
}
}
function simpleReset(url) {
window.location.href = url;
}
//////////// MVC 4 HomeController.cs 用于 SignOut() 操作的代码片段
[AllowAnonymous]
public ActionResult SignOut()
{
try
{
ViewBag.Message = "You are signed out.";
//DELETE SSO Cookie
HttpCookie ssoCookie = new HttpCookie("SMSESSION", "NO");
ssoCookie.Expires = DateTime.Now.AddYears(-1);
ssoCookie.Domain = ".myDomain.com"; //IMPORTANT: you must supply the domain here
Response.Cookies.Add(ssoCookie);
//Look for an existing authorization cookie and kill it
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
authCookie.Value = null;
authCookie = null;
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
// clear authentication cookie
//Overriding the existing FormsAuthentication cookie with a new empty cookie ensures
//that even if the client winds back their system clock, they will still not be able
//to retrieve any user data from the cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Path = FormsAuthentication.FormsCookiePath;
cookie1.Expires = DateTime.Now.AddYears(-1);
cookie1.HttpOnly = true;
Response.Cookies.Add(cookie1);
// clear session cookie
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);
//Explicitly destroy roles object and session
SimpleSessionHandler.myRoles = null;
Session.Clear();
Session.Abandon();
// Invalidate the Cache on the Client Side
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
FormsAuthentication.SignOut();
}
catch (Exception ex) {
//Swallow it
Log.LogError("AUTH COOKIE ERROR: " + ex.Message, ex);
}
//The loginUrl on IWH is actually a LOGOUT page for the SSO cookie
return Redirect(FormsAuthentication.LoginUrl);
}