在按键或鼠标事件上更新票证

时间:2013-01-11 11:12:04

标签: c# jquery asp.net-mvc-3 mouseevent keypress

我在网站上使用C#来使用FormsAuthenticationTicket创建会话。

FormsAuthenticationTicket的票证代码如下

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
                                                                 model.UserName,
                                                                 DateTime.Now,
                                                                 DateTime.Now.AddMinutes(30),
                                                                 false,
                                                                 admin.Role);
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) {   Expires = ticket.Expiration });

网站的每个部分都继承了Site.Master,所以我想在这些行中添加一些内容到Site.Master

$(document).ready(function() {
    $(window).bind('mousemove mouseclick keydown mousewheel', idle);
});

function idle(e) {
    if (window.idleTimeout) {
        clearTimeout(window.idleTimeout);
    }
    <%= FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;  %>
    <%= FormsAuthentication.RenewTicketIfOld(identity.Ticket); %>
    window.idleTimeout = setTimeout(userIdle, 900000);
}
function userIdle() {
    //either do stuff here or trigger the "idle" event
    $(window).trigger('idle');
}

这不起作用。它不能使用FormsIdentity因为我不能做“使用System.Web.Security;”这里。

是否有任何类似的逻辑可用于在按键和鼠标事件等事件上刷新故障单?

仅供参考:如果我去网站的其他部分,门票不会过期,所以这不是问题。只有当人们在填写表格时使用40分钟然后尝试提交表格并失去所有进度。当然我可以增加超时但这不是我想做的。

1 个答案:

答案 0 :(得分:0)

您可以向服务器发送AJAX请求以刷新cookie:

function idle(e) {
    if (window.idleTimeout) {
        clearTimeout(window.idleTimeout);
    }
    $.ajax({
        url: '<%= Url.Action("RenewCookie") %>',
        type: 'POST'
    });
    window.idleTimeout = setTimeout(userIdle, 900000);
}

然后在您的服务器上,您将有一个控制器操作,将执行必要的步骤。但要注意,在每次按键或鼠标滚轮上使用AJAX请求来破坏你的服务器可能是毁灭性的。

相关问题