问题描述
我正在开发一个Iphone应用程序,我需要为此在后台执行自动注销,即在按下Iphone的主页"按钮之后.
I am developing an Iphone app for which I need to perform the automatic logout in the background itself, i.e after the Home Button for Iphone is pressed.
我为会话超时尝试了以下代码,该代码非常适用于台式机.但是该解决方案无法在Iphone的后台运行,因为我必须等待完整的10秒才能重定向到所需的页面.
I have tried the following code for session timeout which works well for desktop. but this solution doesn't work in the background for Iphone as I have to wait complete 10 seconds to get redirected to the desired page.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function(){
var wintimeout;
function SetWinTimeout() {
wintimeout = window.setTimeout("window.location.href='try.html';",10000); //after 5 mins i.e. 5 * 60 * 1000
}
$('body').click(function() {
window.clearTimeout(wintimeout); //when user clicks remove timeout and reset it
SetWinTimeout();
});
SetWinTimeout();
});
</script>
</head>
<body>
<a href = "try.html"> try link </a>
Hey there.. is this working fine?
</body>
</html>
有人可以为此提供解决方案吗?
Can someone give the solution for this?
此外,即使我点击屏幕,上述代码中的会话超时间隔也不会在Iphone上重置,因为我正被重定向到所需的页面.我该如何解决这个问题?
Also the session timeout interval in the above code is not getting resetted on the Iphone as I am being redirected to the desired page even if I am tapping the screen. How can I solve this issue?
推荐答案
这样的事情怎么样...注意:我有几个MVC Razor片段,可以与其他任何内容一起切换.我没有测试过,但这是一个好的开始.非Safari iOS浏览器将忽略pageshow/pagehide事件处理程序,因此这不是针对移动设备的完整解决方案.请随时对此进行改进,以实现更广泛的跨浏览器功能.另外,此解决方案需要使用JQuery cookie插件: https://github.com/carhartl/jquery- Cookie
How about something like this...NOTE: I have a couple of MVC Razor snippets that could be switched out with anything else. I haven't tested this but it's a good start. Non Safari iOS browsers will ignore the pageshow/pagehide event handlers so this is NOT a complete solution for mobile devices. Please feel free to improve upon this for broader cross browser functionality. Also, this solution requires the use of the JQuery cookie plugin: https://github.com/carhartl/jquery-cookie
/////////客户端代码
///////// Client-Side Code
//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()操作
///////////// MVC 4 HomeController.cs code fragment for SignOut() Action
[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);
}
这篇关于自动会话超时iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!