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.
Step 1: Run the application then enter username and password.
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.
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace ClientSideSession.Models
- {
- public class sessionClass
- {
- public bool sessionValue { set; get; }
- }
- }
Step 2: Add JSON method in controller for checking session from client side with the help of “sessionClass” class.
- public JsonResult checkSession()
- {
- sessionClass s = new sessionClass();
- if (Session["UserName"]!=null)
- {
- s.sessionValue = true;
- }
- else
- {
- s.sessionValue = false;
-
- }
- return Json(s, JsonRequestBehavior.AllowGet);
- }
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.
- <script>
-
- $("#btnClick").click(function () {
-
- $.ajax({
-
- type: 'POST',
- dataType: 'json',
- contentType: 'application/json',
- url: '/Home/checkSession',
- data: '{}',
- success: function (data) {
- alert(data.sessionValue);
- if (data.sessionValue==true)
- {
- location.href="/Home/Demo"
- }
- else
- {
- location.href = "/Home/Login"
- }
- },
- error: function (xhr) {
- alert('error');
- }
- });
-
- });
- </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: