How long does a session[] retain data?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















I am trying to use a session but when i need the value back its null?



My website starts off with a login where i put the user name into a Session.



Session["userName"] = login.UserName;


Then the website is redirected to a new page.



return RedirectToAction("Choice", new { token = token });


after witch i use a link to move to my next page.



@Html.ActionLink("Download", "Download", new {token = Model.Token})


Where i direct my code to a action result in the Home controller, the same controller i have my login function.



@Html.ActionLink("Download", "DownloadFile", new { fileid = item.Key, token = Model.Token, platform = "windows", filename = item.Value }, new {@class = "DownloadLink" } )


Where i try call up my session value again.



formMobiApi.GetFile(token, fileid, platform, filename, Session["userName"].ToString(), tasks, taskId, spreadSheetStatus);


Are any of these actions doing anything to make my session value null?



Can i use a session like this?










share|improve this question


















  • 1





    How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

    – Paul Aldred-Bann
    Oct 29 '12 at 10:17











  • I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

    – Pomster
    Oct 29 '12 at 10:48


















7















I am trying to use a session but when i need the value back its null?



My website starts off with a login where i put the user name into a Session.



Session["userName"] = login.UserName;


Then the website is redirected to a new page.



return RedirectToAction("Choice", new { token = token });


after witch i use a link to move to my next page.



@Html.ActionLink("Download", "Download", new {token = Model.Token})


Where i direct my code to a action result in the Home controller, the same controller i have my login function.



@Html.ActionLink("Download", "DownloadFile", new { fileid = item.Key, token = Model.Token, platform = "windows", filename = item.Value }, new {@class = "DownloadLink" } )


Where i try call up my session value again.



formMobiApi.GetFile(token, fileid, platform, filename, Session["userName"].ToString(), tasks, taskId, spreadSheetStatus);


Are any of these actions doing anything to make my session value null?



Can i use a session like this?










share|improve this question


















  • 1





    How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

    – Paul Aldred-Bann
    Oct 29 '12 at 10:17











  • I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

    – Pomster
    Oct 29 '12 at 10:48














7












7








7








I am trying to use a session but when i need the value back its null?



My website starts off with a login where i put the user name into a Session.



Session["userName"] = login.UserName;


Then the website is redirected to a new page.



return RedirectToAction("Choice", new { token = token });


after witch i use a link to move to my next page.



@Html.ActionLink("Download", "Download", new {token = Model.Token})


Where i direct my code to a action result in the Home controller, the same controller i have my login function.



@Html.ActionLink("Download", "DownloadFile", new { fileid = item.Key, token = Model.Token, platform = "windows", filename = item.Value }, new {@class = "DownloadLink" } )


Where i try call up my session value again.



formMobiApi.GetFile(token, fileid, platform, filename, Session["userName"].ToString(), tasks, taskId, spreadSheetStatus);


Are any of these actions doing anything to make my session value null?



Can i use a session like this?










share|improve this question














I am trying to use a session but when i need the value back its null?



My website starts off with a login where i put the user name into a Session.



Session["userName"] = login.UserName;


Then the website is redirected to a new page.



return RedirectToAction("Choice", new { token = token });


after witch i use a link to move to my next page.



@Html.ActionLink("Download", "Download", new {token = Model.Token})


Where i direct my code to a action result in the Home controller, the same controller i have my login function.



@Html.ActionLink("Download", "DownloadFile", new { fileid = item.Key, token = Model.Token, platform = "windows", filename = item.Value }, new {@class = "DownloadLink" } )


Where i try call up my session value again.



formMobiApi.GetFile(token, fileid, platform, filename, Session["userName"].ToString(), tasks, taskId, spreadSheetStatus);


Are any of these actions doing anything to make my session value null?



Can i use a session like this?







asp.net session session-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 29 '12 at 10:11









PomsterPomster

5,11944108183




5,11944108183








  • 1





    How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

    – Paul Aldred-Bann
    Oct 29 '12 at 10:17











  • I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

    – Pomster
    Oct 29 '12 at 10:48














  • 1





    How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

    – Paul Aldred-Bann
    Oct 29 '12 at 10:17











  • I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

    – Pomster
    Oct 29 '12 at 10:48








1




1





How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

– Paul Aldred-Bann
Oct 29 '12 at 10:17





How are your sessions handled? Are they cookie based or inproc? What is your session timeout value in your web.config also?

– Paul Aldred-Bann
Oct 29 '12 at 10:17













I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

– Pomster
Oct 29 '12 at 10:48





I could not find any session timeouts in my web.Config, and i am unsure if my sessions are cookie based or inproc, how would i check?

– Pomster
Oct 29 '12 at 10:48












2 Answers
2






active

oldest

votes


















9














If you are not manually setting your session state in web.config then the defaults you should have are in-process (sessions are stored in the RAM of the web server) and timeout is 20 minutes - see here for more information on session states.



You can manually set your states in your web.config file with the following:



<configuration>
<system.web>
<sessionState mode="InProc" timeout="20"></sessionState>
</system.web>
</configuration>


Now, onto your actual issue. I'm not exactly sure why your session isn't persisting and I can't feasibly find out as I don't have a copy of your code to debug. At a guess I'd say your HttpContext is different from when you initialized the session, to when you're requesting it and so your context key is different. Try changing



Session["userName"].ToString();


to



HttpContext.Current.Session["userName"].ToString();


This looks a little odd at first glance, as you're effectively doing the EXACT same thing. The difference comes down to if the current object doesn't have access to the HttpContext you need to explicitly define which context to read from.



Having taken another look at the code you've provided, there's nothing out of the ordinary - this should work. I really am starting to believe this is an environment / configuration issue. If you're using Form Authentication then check your timeout in your web.config file. Also, if you're hosting in IIS (and not Visual Studio's web server - although you should check this too!) try deleting your application pool and recreating it.






share|improve this answer

































    0














    Your session variable is set up properly and it is being called properly to obtain the value.



    The only thing left to debug is to actually see if login.UserName has an actually value. Because you might just be setting a session variable to null(login.UserName).



    also try adding this to your web.config file if that doesn't work.



    <sessionState mode="InProc" timeout="20" customProvider="DefaultSessionProvider">
    <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
    </providers>
    </sessionState>





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13119520%2fhow-long-does-a-session-retain-data%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      If you are not manually setting your session state in web.config then the defaults you should have are in-process (sessions are stored in the RAM of the web server) and timeout is 20 minutes - see here for more information on session states.



      You can manually set your states in your web.config file with the following:



      <configuration>
      <system.web>
      <sessionState mode="InProc" timeout="20"></sessionState>
      </system.web>
      </configuration>


      Now, onto your actual issue. I'm not exactly sure why your session isn't persisting and I can't feasibly find out as I don't have a copy of your code to debug. At a guess I'd say your HttpContext is different from when you initialized the session, to when you're requesting it and so your context key is different. Try changing



      Session["userName"].ToString();


      to



      HttpContext.Current.Session["userName"].ToString();


      This looks a little odd at first glance, as you're effectively doing the EXACT same thing. The difference comes down to if the current object doesn't have access to the HttpContext you need to explicitly define which context to read from.



      Having taken another look at the code you've provided, there's nothing out of the ordinary - this should work. I really am starting to believe this is an environment / configuration issue. If you're using Form Authentication then check your timeout in your web.config file. Also, if you're hosting in IIS (and not Visual Studio's web server - although you should check this too!) try deleting your application pool and recreating it.






      share|improve this answer






























        9














        If you are not manually setting your session state in web.config then the defaults you should have are in-process (sessions are stored in the RAM of the web server) and timeout is 20 minutes - see here for more information on session states.



        You can manually set your states in your web.config file with the following:



        <configuration>
        <system.web>
        <sessionState mode="InProc" timeout="20"></sessionState>
        </system.web>
        </configuration>


        Now, onto your actual issue. I'm not exactly sure why your session isn't persisting and I can't feasibly find out as I don't have a copy of your code to debug. At a guess I'd say your HttpContext is different from when you initialized the session, to when you're requesting it and so your context key is different. Try changing



        Session["userName"].ToString();


        to



        HttpContext.Current.Session["userName"].ToString();


        This looks a little odd at first glance, as you're effectively doing the EXACT same thing. The difference comes down to if the current object doesn't have access to the HttpContext you need to explicitly define which context to read from.



        Having taken another look at the code you've provided, there's nothing out of the ordinary - this should work. I really am starting to believe this is an environment / configuration issue. If you're using Form Authentication then check your timeout in your web.config file. Also, if you're hosting in IIS (and not Visual Studio's web server - although you should check this too!) try deleting your application pool and recreating it.






        share|improve this answer




























          9












          9








          9







          If you are not manually setting your session state in web.config then the defaults you should have are in-process (sessions are stored in the RAM of the web server) and timeout is 20 minutes - see here for more information on session states.



          You can manually set your states in your web.config file with the following:



          <configuration>
          <system.web>
          <sessionState mode="InProc" timeout="20"></sessionState>
          </system.web>
          </configuration>


          Now, onto your actual issue. I'm not exactly sure why your session isn't persisting and I can't feasibly find out as I don't have a copy of your code to debug. At a guess I'd say your HttpContext is different from when you initialized the session, to when you're requesting it and so your context key is different. Try changing



          Session["userName"].ToString();


          to



          HttpContext.Current.Session["userName"].ToString();


          This looks a little odd at first glance, as you're effectively doing the EXACT same thing. The difference comes down to if the current object doesn't have access to the HttpContext you need to explicitly define which context to read from.



          Having taken another look at the code you've provided, there's nothing out of the ordinary - this should work. I really am starting to believe this is an environment / configuration issue. If you're using Form Authentication then check your timeout in your web.config file. Also, if you're hosting in IIS (and not Visual Studio's web server - although you should check this too!) try deleting your application pool and recreating it.






          share|improve this answer















          If you are not manually setting your session state in web.config then the defaults you should have are in-process (sessions are stored in the RAM of the web server) and timeout is 20 minutes - see here for more information on session states.



          You can manually set your states in your web.config file with the following:



          <configuration>
          <system.web>
          <sessionState mode="InProc" timeout="20"></sessionState>
          </system.web>
          </configuration>


          Now, onto your actual issue. I'm not exactly sure why your session isn't persisting and I can't feasibly find out as I don't have a copy of your code to debug. At a guess I'd say your HttpContext is different from when you initialized the session, to when you're requesting it and so your context key is different. Try changing



          Session["userName"].ToString();


          to



          HttpContext.Current.Session["userName"].ToString();


          This looks a little odd at first glance, as you're effectively doing the EXACT same thing. The difference comes down to if the current object doesn't have access to the HttpContext you need to explicitly define which context to read from.



          Having taken another look at the code you've provided, there's nothing out of the ordinary - this should work. I really am starting to believe this is an environment / configuration issue. If you're using Form Authentication then check your timeout in your web.config file. Also, if you're hosting in IIS (and not Visual Studio's web server - although you should check this too!) try deleting your application pool and recreating it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 29 '12 at 11:16

























          answered Oct 29 '12 at 11:02









          Paul Aldred-BannPaul Aldred-Bann

          4,49342844




          4,49342844

























              0














              Your session variable is set up properly and it is being called properly to obtain the value.



              The only thing left to debug is to actually see if login.UserName has an actually value. Because you might just be setting a session variable to null(login.UserName).



              also try adding this to your web.config file if that doesn't work.



              <sessionState mode="InProc" timeout="20" customProvider="DefaultSessionProvider">
              <providers>
              <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
              </providers>
              </sessionState>





              share|improve this answer




























                0














                Your session variable is set up properly and it is being called properly to obtain the value.



                The only thing left to debug is to actually see if login.UserName has an actually value. Because you might just be setting a session variable to null(login.UserName).



                also try adding this to your web.config file if that doesn't work.



                <sessionState mode="InProc" timeout="20" customProvider="DefaultSessionProvider">
                <providers>
                <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
                </providers>
                </sessionState>





                share|improve this answer


























                  0












                  0








                  0







                  Your session variable is set up properly and it is being called properly to obtain the value.



                  The only thing left to debug is to actually see if login.UserName has an actually value. Because you might just be setting a session variable to null(login.UserName).



                  also try adding this to your web.config file if that doesn't work.



                  <sessionState mode="InProc" timeout="20" customProvider="DefaultSessionProvider">
                  <providers>
                  <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
                  </providers>
                  </sessionState>





                  share|improve this answer













                  Your session variable is set up properly and it is being called properly to obtain the value.



                  The only thing left to debug is to actually see if login.UserName has an actually value. Because you might just be setting a session variable to null(login.UserName).



                  also try adding this to your web.config file if that doesn't work.



                  <sessionState mode="InProc" timeout="20" customProvider="DefaultSessionProvider">
                  <providers>
                  <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
                  </providers>
                  </sessionState>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 14:34









                  Eric C. SaffordEric C. Safford

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13119520%2fhow-long-does-a-session-retain-data%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine