如何避免重置特定请求的owin cookie超时

时间:2018-05-10 05:31:00

标签: c# asp.net asp.net-mvc asp.net-identity owin

我有一个使用Asp.net Identity的应用程序,启用了滑动过期。当用户登录时,我将使用WEB api从服务器获取cookie过期时间,并在JavaScript变量中设置会话超时.java脚本计时器开始运行。如果用户持续进行操作,则不会显示pop。如果用户处于空闲状态,在显示会话过期弹出窗口之前,我将再次向服务器发出请求并获取cookie过期时间(有时候用户可以使用系统而不刷新,仅执行ajax请求。)我将检查登录基于我将显示过期弹出窗口的当前超时超时。

但是当我检查这个时,cookie时间已经重置。如何避免在特定请求时重置cookie超时?

    [HttpGet]      
    public int GetCookieExpireTime()
    {
        var identity = User.Identity as ClaimsIdentity;
        var claimType = "myExpireUtc";  
        if (identity != null && identity.HasClaim(c => c.Type == claimType))
        {
            var expireOn = identity.FindFirstValue(claimType);

            DateTimeOffset currentUtc = DateTimeOffset.UtcNow;
            DateTimeOffset? expireUtc = new DateTimeOffset(long.Parse(expireOn), TimeSpan.Zero);

            var remaining = (expireUtc.Value - currentUtc).TotalSeconds;

            return Convert.ToInt32(remaining);
        }
        return 0;
    }


function ShowPendingTimeoutDialog() {
$.ajax({
    type: "GET",
    url: loc + '/Home/GetCookieExpireTime',
    success: function (timeout) {
        var warnSec = msecWarning / 1000;
        //reset session timer if warn sec less than total time out time
        if (warnSec < timeout) {
            getCookieExpireTime();
        }
            //otherwise show session popup
        else {
            diag.dialog({
                autoOpen: false,
                title: "Session About to Expire",
                closeText: "hide",
                resizable: false,
                draggable: false,
                modal: true,
                buttons: {
                    Continue: function () {
                        ResetTimeout();
                    }
                },
                create: function () {
                    $(this).closest('div.ui-dialog')
                           .find('.ui-dialog-titlebar-close')
                           .click(function (e) {
                               ResetTimeout();
                           });
                    $(this).closest(".ui-dialog")
                .find(".ui-dialog-titlebar-close")
                .html("<span class='ui-button-icon-primary ui-icon ui-icon-closethick'></span>");
                }
            });
            UpdateTimeoutMessage();
            diag.dialog("open");
        }

    },
    failure: function () {

    }
});

function ResetTimeout() {
diag.dialog("close");
$.ajax({
    type: "GET",
    url: loc + '/KeepSessionAlive.ashx',
    success: function () {
        getCookieExpireTime();
    },
    failure: function () {

    }
});

0 个答案:

没有答案