0

In MVC application, I have made most of the CRUD operations call using AJAX.

Problem is , when session gets timeout then, its unable to redirect to session time out page.

Below is the related code which works fine when there is no AJAX call.

 [AttributeUsage(AttributeTargets.Class)] //| AttributeTargets.Method
 public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter
 {
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
          \\check if session is null then redirect to session time out page.
    }
 }

For login, i just check against the databsae, there is nothing for memebrship provider.

     [HttpPost]
      public JsonResult Login(string username, string password, bool RememberMe)
     {
        try
        {
         UserDTO accDTO = new UserDTO ()
            {
                UsernAme = username,
                Password = DataEncryption.EncryptPassword(password)
            };

            UserDTO AccDTO = _iAccount.UserAuthentication(accDTO);

            if (AccDTO != null)
            {
                Session["UserId"] = 1;
                Session["userdto_Session"] = AccDTO;
                // Remember me
                HttpCookie myCookie = new HttpCookie("appCookie");
                //chkRememberMe.Checked;
                if (RememberMe)
                {
                    myCookie.Values.Add("username", username);
                    myCookie.Values.Add("password", password);
                    myCookie.Expires = DateTime.Now.AddMinutes(20);
                }
                else
                {
                    myCookie.Values.Add("username", string.Empty);
                    myCookie.Values.Add("password", string.Empty);
                    myCookie.Expires = DateTime.Now.AddMinutes(5);
                }
                Response.Cookies.Add(myCookie);
                // Remember me

                return Json(AccDTO.SID, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null);
            }
        }
        catch (Exception ex)
        {

        }
        return null;
    }

AJAX Call is made for login and all CRUD operation are using $.AJAX({...});.

ssilas777
  • 9,672
  • 4
  • 45
  • 68
dsi
  • 3,199
  • 12
  • 59
  • 102
  • Show the code for ajax call – ssilas777 Oct 30 '14 at 04:46
  • check this post:http://stackoverflow.com/questions/26638368/asp-net-mvc-redirect-out-of-a-partial-view-from-controller-to-a-full-view-from-a – Ehsan Sajjad Oct 30 '14 at 05:43
  • @EhsanSajjad I am using normal login - authentication on database. so, [Authorize] attribute can't be handle or work as mentioned in above stackoverflow link. – dsi Oct 30 '14 at 06:03
  • @Dhaval see the `onActionExecuting()` block for how to redirect in ajax call, leave the attribute thing thing to see `OnActionExecuting()` – Ehsan Sajjad Oct 30 '14 at 06:26

1 Answers1

0

Ajax requests should be handled at client side itself, Try this

Attribute:

 [AttributeUsage(AttributeTargets.Class)] //| AttributeTargets.Method
 public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter
 {     
   public void OnActionExecuting(ActionExecutingContext filterContext)
        {

           //TO HANDLE AJAX REQUESTS
           if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                //If session is null
                filterContext.Result = new JsonResult
                {
                    Data = new 
                    { 
                        // put a message which sentto the client
                        message = "Session Time out" 
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }else{
                   //
            }
        }
  }

In your javascript

$.ajax(function(){
   url:"",
   success: function (result) {            
               if(result.message == "Session Time out"){
                //Session timed out handle it
                //window.location.href = session timeout url
               } 
            },

});
ssilas777
  • 9,672
  • 4
  • 45
  • 68
  • Below code will call when do ajax call ? `[AttributeUsage(AttributeTargets.Class)] //| AttributeTargets.Method public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) {` – dsi Oct 30 '14 at 05:21
  • I didn't get you properly, although edited my answer for more clarity. – ssilas777 Oct 30 '14 at 05:48
  • I just want to ask, when we call action using AJAX call , though it works to call filter attribute `FilterAttribute` - onexecuting method ? – dsi Oct 30 '14 at 06:05
  • OnActionExecuting will work for ajax calls also. Please try the approach and check if it is working for you. – ssilas777 Oct 30 '14 at 07:22