Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按键或鼠标事件时续订票据_C#_Jquery_Asp.net Mvc 3_Mouseevent_Keypress - Fatal编程技术网

C# 按键或鼠标事件时续订票据

C# 按键或鼠标事件时续订票据,c#,jquery,asp.net-mvc-3,mouseevent,keypress,C#,Jquery,Asp.net Mvc 3,Mouseevent,Keypress,我正在网站上使用C#通过FormsAuthenticationTicket创建会话 FormsAuthenticationTicket的票证代码如下 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.UserName,

我正在网站上使用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');
}
$(文档).ready(函数(){
$(窗口).bind('mousemove mouseclick keydown mouseweel',idle);
});
功能空闲(e){
if(window.idleTimeout){
clearTimeout(window.idleTimeout);
}
window.idleTimeout=setTimeout(userIdle,900000);
}
函数userIdle(){
//要么在这里执行任务,要么触发“空闲”事件
$(window.trigger('idle');
}
这是行不通的。它不能使用表单实体,因为我不能在这里“使用System.Web.Security”

在按键和鼠标事件等事件中,我是否可以使用类似的逻辑来不断刷新票据


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

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

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

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

您还有其他建议可以减轻服务器的压力吗?是否可以在客户端执行此操作?不,续订表单身份验证cookie的唯一可能方法是向服务器发送请求。因此,如果希望执行较少的请求,可以在客户机上维护一个事件列表,并定期检查该列表中是否有事件,然后只发送一个AJAX请求。