How can we read or get jquery session timeout from web.config so that we can handle session expiration in mvc

How to get session value from webconfig via ajax call

I tried the following code

<script language="javascript" type="text/javascript">
       var sessionTimeoutWarning = 
    "<%= System.Configuration.ConfigurationSettings.AppSettings
    ["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";

        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

        function SessionWarning() {
var message = "Your session will expire in another " + 
    (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + 
    " mins! Please Save the data before the session expires";
alert(message);
        }
</script>
+4
source share
2 answers

You actually do it in MVC; You may need to use syntax for this "@". Please try this, it may be useful.

 <script language="javascript" type="text/javascript">
     var sessionTimeoutWarning =    @System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString();
     var sessionTimeout = @Session.Timeout;    
     var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

   function SessionWarning() {
     var message = "Your session will expire in another " + (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + 
                   " mins! Please Save the data before the session expires";
     alert(message);
   }
</script>
+5
source

Step 1. Create an ActionMethod / Function in the controller

public string IsUserLoggedIn()
        {
            if (Session["UserId"] != null) // check session here
                return "true";
            else
                return "false";
        }

Step 2: Create an ajax call. if returns true, then the user is not registered otherwise.

function IsUserLoggedIn() {
        var lsValue = true;
        $.ajax({
            type: "Post",
            url: "IsUserLoggedIn",
            datatype: "json",
            async: false,
            success: function (foData) {
                if (foData == "true")
                    lsValue = true;
                else
                    lsValue = false;
            },
            error: function (xhr, ajaxOptions, thrownError) {
            },
            complete: function (e) {
            }
        });
        return lsValue;
    }
0
source

All Articles