1. Create action result method in controller.
In this action method check user authenticated and session.If session is null or user is not authenticated then
return json result(redirect on Logout action) otherwise return null.
- public ActionResult IsSessionInPro()
-
- {
-
- if ((User.Identity.IsAuthenticated) || SessionManager.Instance.CurrentUser == null)
-
- {
-
- if (HttpContext.Response.StatusCode == 401)
-
- {
-
- HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
-
- HttpContext.Response.End();
-
- return new JsonResult {
- Data = new {
- message = "logout"
- }, JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
-
- }
-
- }
-
- return null;
-
- }
2. ajaxStart funcation excute before ajax call
- $(document).ajaxStart(function()
- {
-
- $.ajax({
-
- type: 'POST',
-
- url: '@Url.Action("IsSessionInPro", "ControllerName")',
-
- async: false,
-
- success: function(data)
- {
-
- if (data == "logout")
- {
-
- window.location.href = '@Url.Action("ActionName", "ControllerName")';
-
- }
-
- }
-
- });
-
- });
3. ajaxError function call when any arror eccure in ajax call.
- $(document).ajaxError(function(e, xhr)
- {
-
- if (xhr.status == 401)
-
- window.location.href = '@Url.Action("ActionName", "ControllerName")';
-
- });