简体   繁体   中英

Asp.net MVC Session timeout condition based handling

My MVC web application uses asp.net session management. Session has default timeout 20mins. On session timeout, user is redirected to the home page. My application uses SessionTimeout ActionFilterAttribute to handle session timeout. SessionState is stored in server.

Problem : Even if the session timout happens, when the user returns to a particular action method, I need to renew a session for that user and allow to continue instead of redirecting to home page.

I have tried to use the OnActionExecuting method in the SessionTimeout ActionFilterAttribute to identify the session timeout & if the action in the request is that particular action method, then allow the user to continue to that action.

But it seems to be not working. It just redirects the user to the Home page. I am not sure how to proceed.

Session have bad problems like timeout and refresh not available, do authentication using forms authentication you can choose this custom authentication sample

Or Else use cookies

        HttpCookie MyCookie = new HttpCookie("MyCookie");
        // for remveing if already Exists adding new;
        Request.Cookies.Remove("MyCookie");
        if (Request.Cookies["MyCookie"] == null)
        {

            MyCookie["id"] = id.ToString();
            Response.Cookies.Add(MyCookie);
        }
        else
        {
            MyCookie["id"] = id.ToString();
            // Request.Cookies.Remove("MyCookie");
            Response.Cookies.Set(MyCookie);
        }

        // retries 
        int id = Convert.ToInt32(Request.Cookies["MyCookie"]["id"]);

Thanks for your responses.

  1. "Session cannot be renewed" once it has expired.
  2. Instead of renewing the session, create a new session in the ActionFilters Attribute (SessionTimeout).

The solution for my problem is to create a new session and re-link it with the domain object/user so that the user can continue his journey. I have done this in my SessionTimeout ActionFilterAttribute, to create new session for only a particular request which has the particular controller/action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM