Check Session Status From Client Side Using Ajax In ASP.NET MVC

Introduction

This article explains about session and how to check session status in client side using Ajax in ASP.NET MVC.

Definition

Session is server side variable. It is used to store and retrieve from any page in current user level.

Problem in Session when use in jQuery

Session value can be accessed in Java script and jQuery. If we update or change the session value in server side, it is not affected in JavaScript and jQuery. Session maintains the value which value is stored the first time in jQuery and JavaScript. If session times out values automatically are removed in  theserver side but client side is not removed so in this situation session is not working correctly or it makes the wrong result.

In web.config assigned session times outin  one minute.

config

Step 1: Run the application then enter username and password.

application

Step 2:

If password and username is correct then it redirects to Demo Page. When clicking  Redirect button at that time it redirects to another page. While button click is needed  to check session status, if session is not null redirect any other required page otherwise redirect to login page.

First time session is true so it redirected to Check Session Page. In web.congig file assigned session time out one minute.

Session

Step 3:

Again click Redirect button after one minute. While clicking redirect button after one minute, now session value is null because session times out again so go same page do not go to the login page.



Here we click Redirect button each time session value true, because once we assign session value in JavaScript or jQuery it's again not updated, so each time  it's the session value which is assigned first time.

This is the problem in Jquery and java script.

Solution

In ASP.Net MVC we using Ajax each time to check session status from client side to server side, that is from Jquery to controller. For example when redirecting from one page to another page on button click check session status using Ajax.

Step 1: Add class in model for checking session from client side as follows.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace ClientSideSession.Models  
  7. {  
  8.     public class sessionClass  
  9.     {  
  10.         public bool sessionValue { setget; }  
  11.     }  
  12. }  
Step 2: Add JSON method in controller for checking session from client side with the help of “sessionClass” class.
  1. public JsonResult checkSession()  
  2. {  
  3.      sessionClass s = new sessionClass();  
  4.      if (Session["UserName"]!=null)  
  5.      {   
  6.          s.sessionValue = true;  
  7.      }  
  8.      else  
  9.      {  
  10.           s.sessionValue = false;  
  11.            
  12.      }  
  13.      return Json(s, JsonRequestBehavior.AllowGet);  
  14. }  
Step 3:

In button click we need to check session status. While button clicking with the help of ajax check session status in checkSession method. Here if session value is not null then assign value true in sessionClass’s prosperities otherwise assign the value false.
  1. <script>  
  2.      
  3.     $("#btnClick").click(function () {  
  4.   
  5.         $.ajax({  
  6.   
  7.             type: 'POST',  
  8.             dataType: 'json',  
  9.             contentType: 'application/json',  
  10.             url: '/Home/checkSession',  
  11.             data: '{}',  
  12.             success: function (data) {  
  13.                 alert(data.sessionValue);  
  14.                 if (data.sessionValue==true)  
  15.                 {  
  16.                     location.href="/Home/Demo"  
  17.                 }  
  18.                 else  
  19.                 {  
  20.                     location.href = "/Home/Login"  
  21.                 }  
  22.             },  
  23.             error: function (xhr) {  
  24.                 alert('error');  
  25.             }  
  26.         });  
  27.   
  28.     });  
  29.  </script>  
In Button click event we need to add above the code each time and check session status from client side to server side. Wherever we need session status in client side, could follow like above code.

Conclusion

I hope this step by step method help you how to check session status from client side to server side using jQuery in ASP.NET MVC.
 
Read more articles on ASP.NET


Similar Articles