locked
Session variables lost after the call of Response.Redirect method RRS feed

  • Question

  • User56714588 posted

    Hi Everyone,

    In my asp.net application after I login, my session variables, which I set in login page, are getting lost after the call to Response.Redirect method.

    After googling I found the below link elaborate the cause of the problem.

    http://weblogs.asp.net/bleroy/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_

    I really couldn't get the solution suggested in the above link.

    I didn't get the error or the issue was not there for a month. It suddenly appeared yesterday and I'm unable to use my session variables if use Response.Redirect method, even with the second argument with 'false', ie. Response.Redirect(URL,false);

    Why I got the error all of a sudden? And what is the solution?

    Thursday, June 9, 2016 6:15 AM

Answers

  • User-2057865890 posted

    Hi ArunCode47,

    Have you tried setting cookieless="true" in web.config? Check that enableSessionState hasn't been set to false in the web.config.

    <configuration>
      <system.web>
    <pages enableSessionState="true"> <sessionState cookieless="true" regenerateExpiredSessionId="true" timeout="30" /> </system.web> </configuration>

    ASP.NET Session State Overview

    Cookieless ASP.NET

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 18, 2016 7:45 AM

All replies

  • User-271186128 posted

    Hi ArunCode47,

    According to your description, I create a sample on my side, it seems that everything works well.

    I suggest you could refer to the following link: http://stackoverflow.com/questions/3515947/losing-session-state

    A number of things can cause session state to mysteriously disappear.

    1. Your sessionState timeout has expired
    2. You update your web.config or other file type that causes your AppDomain to recycle
    3. Your AppPool in IIS recycles
    4. You update your site with a lot of files, and ASP.NET proactively destroys your AppDomain to recompile and preserve memory.

    Besides, I suggest you check the Session State Mode. You can use StateServer or   SqlServer session state mode.

    https://support.microsoft.com/en-sg/kb/324772

    Best regards,
    Dillion

    Thursday, June 9, 2016 8:09 AM
  • User56714588 posted

    Thanks Zhi Lv,

    Suggestion are telling about losing session after leaving the application ideally for quite long time.

    But in my case I'm unable to carry a session variable from the login page itself and want to use it in the next page.

    If I use Response.Redirect method with single argument(URL), I'm getting below exception and my session variables are lost.

    "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"

    If I use Response.Redirect() with 2nd argument as 'false', I'm not getting any exception, but my session variables are lost.

    Any way my session variables are getting lost. And this is happening all of a sudden. Untill yesterday afternoon application was working fine. I didn't do any change in web.config file or IIS server.

    Thursday, June 9, 2016 9:20 AM
  • User-271186128 posted

    Hi ArunCode47,

    For the second argument, you can refer to this article:

    https://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx

    Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"

    As for this issue, you can check this link:

    http://weblogs.asp.net/varadam/unable-to-evaluate-expression-because-the-code-is-optimized-or-a-native-frame-is-on-top-of-the-call-stack

    Best regards,
    Dillion

    Friday, June 10, 2016 10:02 AM
  • User56714588 posted

    Hi Dillion,

    If I use Response.Redirect() with 2nd argument as 'false', I'm not getting the above mentioned exception, but my session variables are lost.

    I want to carry the session variable and also use the redirect method.

    This error occurs in my test instance, but in my production instance, I'm able to get session variables even if i use Response.Redirect() method.

    What could be the possible reason for this issue???????

    Friday, June 10, 2016 11:14 AM
  • User-2057865890 posted

    Hi ArunCode47,

    Have you tried setting cookieless="true" in web.config? Check that enableSessionState hasn't been set to false in the web.config.

    <configuration>
      <system.web>
    <pages enableSessionState="true"> <sessionState cookieless="true" regenerateExpiredSessionId="true" timeout="30" /> </system.web> </configuration>

    ASP.NET Session State Overview

    Cookieless ASP.NET

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 18, 2016 7:45 AM