0

Iphone のホーム ボタンが押された後、バックグラウンド自体で自動ログアウトを実行する必要がある Iphone アプリを開発しています。

デスクトップでうまく機能するセッションタイムアウトの次のコードを試しました。しかし、このソリューションは、目的のページにリダイレクトされるまで 10 秒待たなければならないため、Iphone のバックグラウンドでは機能しません。

<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>

誰かがこれに対する解決策を教えてもらえますか?

また、画面をタップしても目的のページにリダイレクトされるため、上記のコードのセッション タイムアウト間隔は Iphone でリセットされません。この問題を解決するにはどうすればよいですか?

4

3 に答える 3

0

このようなものはどうですか... 注:他のものに切り替えることができる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;
    }

///////////// SignOut() アクションの MVC 4 HomeController.cs コード フラグメント

    [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);

    }
于 2015-02-06T21:23:59.080 に答える
0

たぶん、このようなものですか?

<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(){
    document.lastActivity = new Date().getTime();
    document.threshold = 20000; // some value
    // and set an interval to keep an eye on last activity
    var interval = setInterval(/* something here */);
    $('body').on('click keydown', function() {
        document.lastActivity = new Date().getTime(); // keep resetting this
    });
}).on('focus', function () {
    var timeSince = new Date().getTime() - document.lastActivity;
    if (timeSince > document.threshold) {/* do stuff here */}
});
</script>
</head>
<body>
<a href = "try.html"> try link </a>
Hey there.. is this working fine?
</body>
</html>

アイデアは、最後のアクティビティからの経過時間を計算することです。iOS デバイスは、ブラウザーがバックグラウンドにあるときに JS を一時停止します。これには、正当な理由があると思います。

于 2014-03-13T09:18:00.997 に答える
0

iPhone Sdk は、バックグラウンドに入ると 5 秒後にすべてのタスクをハングします。アプリがオーディオ再生位置の更新などのマルチタスクをサポートしている場合、バックグラウンド タスクをサポートします。私があなたに提供できる1つの解決策は、バックグラウンドに入るときの時間を節約し、フォアグラウンドに来るときの時間を節約します。ログアウトする時間よりも長く計算された場合は、そのために必要なサービスにアクセスしてください。

于 2013-05-13T12:46:11.627 に答える