locked
How to detect if a session variable has expired and define it again RRS feed

  • Question

  • User206383436 posted

    I need to use one session variable in my ASP.NET MVC 5 project and keep the default timeout.
    What is the value of an expired session variable?
    If such a session variable has expired and I need it again, is it enough to re-define?
    Is the code example below correct?
    What is the correct code to detect if the session variable expired and, if so, re-define it again.

    if (Session["var"] has expired)
    {
        Session["var"] = some value;
    }
    

    I will very much appreciate your feedback.

    Best regards,
    Jorge Maldonado

    Thursday, June 27, 2019 11:42 PM

Answers

  • User-1174608757 posted

    Hi JORGEMAL,

    According to your description, if the session value has expired , the value will be null. So ,you could use code as below:

    if (Session["var"] == null )
    {
          Session["var"] = some value;
    }

    If you want to redefine it , you could set somevalue as you want.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 2:23 AM

All replies

  • User-1174608757 posted

    Hi JORGEMAL,

    According to your description, if the session value has expired , the value will be null. So ,you could use code as below:

    if (Session["var"] == null )
    {
          Session["var"] = some value;
    }

    If you want to redefine it , you could set somevalue as you want.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 2:23 AM
  • User-1038772411 posted

    Hi, JORGEMAL

    On page_load you can check any of them and carry out the appropriate action i.e. Response.Redirect. However you say in the Web.Config you can check when the authentication cookie is gone, so can you not carry out an action on this?

    Please Refer Below Link, Hope you will get as you want

    https://stackoverflow.com/questions/4133089/how-to-detect-if-sessionstate-has-expired

    Thanks.

    Tuesday, July 9, 2019 11:49 AM