MVC会话并延长超时时间W / O丢失会话变量

时间:2017-05-25 13:33:33

标签: jquery asp.net session model-view-controller asp.net-mvc-5

哦,美妙的MVC世界......

我正在尝试为我的网站设置超时脚本。目前它似乎工作,但如果用户想延长超时时间然后移动到另一个页面,它似乎已经丢失了我保存的所有会话变量。

FilterConfig.cs代码:

public class IsAuthenticatedAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (rowCnt >= 1) //Making sure user has permissions to even view this site
            {
                System.Diagnostics.Debug.WriteLine("Current timeout: " + HttpContext.Current.Session.Timeout);
                System.Diagnostics.Debug.WriteLine("Session timeout: " + HttpContext.Current.Session["timeoutDate"]);

                if (HttpContext.Current.Session.Timeout == 20) //20 is the default timeout setting
                {
                    //It's the first time the user has visited the page so set timeout!
                    filterContext.HttpContext.Session.Clear();
                    filterContext.HttpContext.Session.RemoveAll();

                    HttpContext.Current.Session.Timeout = 1;
                    HttpContext.Current.Session["sessionVars"] = new initSessionVars().getSVars;
                    sessionVars sVars = HttpContext.Current.Session["sessionVars"] as sessionVars;

                    //Send them to the main page in order to populate the sessions and needed data
                    filterContext.Result = new RedirectResult("/"); 
                } else
                {
                    if (HttpContext.Current.Session["timeoutDate"] == null)
                    {
                        //No timeout has been saved so save it
                        int sessionTimeout = HttpContext.Current.Session.Timeout;

                        HttpContext.Current.Session["timeoutDate"] = DateTime.Now.AddMinutes(sessionTimeout);
                    }
                }                
            } else
            {
                //User was not found so they are NOT good to go!
                filterContext.HttpContext.Response.Redirect("~/Account/Unauthorized");
            }
        }
    }

我正在使用一些jQuery来检查超时,并在重置会话前10秒向用户显示一个消息框,然后将它们发送到"会话。页。

_Layout.cshtml代码:

 var idleTime = 0;
 var secondsLeft = 10;
 var sTimer;
 var idleInterval;

    $(document).ready(function () {
        idleInterval = setInterval(timerIncrement, @Html.Raw((Session.Timeout * 60) * 1000));

        $(".sleepy-close, .sleepy-wake-up").click(function () {
            $(".sleepy-overlay").fadeOut('fast');
            clearInterval(sTimer);
            idleTime = 0;

            $.ajax({
                url: 'Home/sessionChk',
                type: 'POST',
                cache: false,
                dataType: 'json',
                data: {
                    continueSession: true
                },
                success: function (responce) {
                    console.log('done');
                    idleInterval = setInterval(timerIncrement, responce);
                    setVars();
                },
                error: function (responce) {
                    showNotify('error', 'An error of<br/>' + responce + '<br/>Has occurred. Please get in touch with me about this error.', 20000);
                }
            });
        });

        $('.sleepy-modal').click(function (event) {
            event.stopPropagation();
        });
    });

    function setVars() {
        idleTime = 0;
        secondsLeft = 10;
        $('#timer').html(secondsLeft);
    }

    function timerIncrement() {
        idleTime++;

        if (idleTime > 1) {
            $('.sleepy-overlay').fadeIn('slow');
            clearInterval(idleInterval);
            sessionTimer();
        }
    }

    function sessionTimer() {
        sTimer = setInterval(function () {
            $('#timer').html(secondsLeft);
            secondsLeft--;

            if (secondsLeft <= 0) {
                clearInterval(sTimer);

                $.ajax({
                    url: 'Home/sessionChk',
                    type: 'POST',
                    cache: false,
                    dataType: 'json',
                    data: {
                        continueSession: false
                    },
                    success: function (responce) {
                        //clear the full screen for user to input new data
                        console.log('done');
                    },
                    error: function (responce) {
                        showNotify('error', 'An error of<br/>' + responce + '<br/>Has occurred. Please get in touch with me about this error.', 20000);
                    }
                });
            }
        }, 1000);
    }

如果用户点击&#34;保持清醒&#34;按钮然后它向 Home / sessionChk 发送一个AJAX请求,该请求为 continueSession 发送 true

HomeController.cs sessionChk代码:

public class HomeController : Controller
{ 
   [HttpPost]
   public void sessionChk(bool continueSession)
   {
       ActionExecutingContext filterContext = new ActionExecutingContext();

       if (continueSession)
       {
           //Add more time to the session timeout
           HttpContext.Session.Timeout = 31;
       }
       else
       {
           //Lets go to the sessions exspiered page
           filterContext.Result = new RedirectResult("/");
       }
   }
}

因此,在增加了31分钟后,我继续在网站内选择另一页访问。一旦该页面开始加载它告诉我一个会话变量我试图加载不再存在.....

在1分钟内会话开始时,页面第一次加载我可以浏览网站内的任何页面,所有会话都很好并加载正确的数据,所以我知道他们在开始时有数据。

那么我做错了什么,因为当我延长超时时间时似乎没有安全的会话数据?

0 个答案:

没有答案