如何在asp.net mvc中重置会话超时

时间:2022-05-02 05:52:15

I want to Reset Session Timeout when user is still active.

我想在用户仍然处于活动状态时重置会话超时。

  1. In my scenario my session timeout is 20 min., when session time is reached before 10 seconds i am showing a dialog to confirm the user as "Session is going to time out, Do you want to stay in ??". If user is click yes, i want to continue my session with again 20 minutes start from 21st minute, because as per requirement i am saving user session time in database table.

    在我的场景中,我的会话超时为20分钟,当会话时间在10秒之前到达时,我将显示一个对话框,以确认用户“会话将超时,您想要留在其中吗?”如果用户点击“是”,我希望在第21分钟的20分钟内继续我的会话,因为根据需求,我在数据库表中节省了用户会话时间。

  2. can we set timer for session timeout.

    我们可以设置会话超时的计时器。

So, Please help me anyone, how to reset session timeout??

请帮我个忙,如何重置会话超时?

1 个解决方案

#1


8  

Write the jQuery script like:

编写jQuery脚本如下:

 (document).ready(function () {


        $("#divdialog").dialog({
            autoOpen: false,

            resizable: false,
            modal: true,
            title: "Session Timeout",

            buttons: {
                Yes: function () {
                    $.ajax({
                        url: '/<your controller>/SessionTimeout', // Redirects to action method for every 20 minutes.

                        dataType: "json",
                        type: "GET",
                        error: function () {
                            alert(" An error occurred.");
                        },
                        success: function (data) {
                            $("#divdialog").dialog('close');
                            display("stop");

                        }
                    });
                },
                Logout: function () {
                    location.href = '/<your controller>/Logout';
                }
            }
        });


    });

    function myFunction() { // Fires every 20 minutes

        setInterval(function () {
            $("#divdialog").dialog('open');

        }, 1200000);
    }

and Add Action Method in your controller like:

并在控制器中添加动作方法,如:

  public ActionResult SessionTimeout()
    {

        Session.Timeout = Session.Timeout + 20;

        return Json("",JsonRequestBehavior.AllowGet);
    }

Hope above technique helps you.

希望以上技巧能帮助你。

#1


8  

Write the jQuery script like:

编写jQuery脚本如下:

 (document).ready(function () {


        $("#divdialog").dialog({
            autoOpen: false,

            resizable: false,
            modal: true,
            title: "Session Timeout",

            buttons: {
                Yes: function () {
                    $.ajax({
                        url: '/<your controller>/SessionTimeout', // Redirects to action method for every 20 minutes.

                        dataType: "json",
                        type: "GET",
                        error: function () {
                            alert(" An error occurred.");
                        },
                        success: function (data) {
                            $("#divdialog").dialog('close');
                            display("stop");

                        }
                    });
                },
                Logout: function () {
                    location.href = '/<your controller>/Logout';
                }
            }
        });


    });

    function myFunction() { // Fires every 20 minutes

        setInterval(function () {
            $("#divdialog").dialog('open');

        }, 1200000);
    }

and Add Action Method in your controller like:

并在控制器中添加动作方法,如:

  public ActionResult SessionTimeout()
    {

        Session.Timeout = Session.Timeout + 20;

        return Json("",JsonRequestBehavior.AllowGet);
    }

Hope above technique helps you.

希望以上技巧能帮助你。