0

I am creating a C# MVC5 Application. I have mentioned session timeout to be 60 minutes in web.config but it's not working and sessions break after couple of minutes.

Here is the code I added in web.config:

<system.web>
    <sessionState mode="InProc" timeout="60"></sessionState>
    <httpRuntime targetFramework="4.5" maxRequestLength="25360" />
    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2" />
    </authentication>    
    <!--<authentication mode="None" />-->
    <compilation debug="true" targetFramework="4.5" />
</system.web>
przno
  • 3,476
  • 4
  • 31
  • 45

2 Answers2

0

I think your misusing the mode section in here.

<system.web>
    <sessionState timeout="60"></sessionState>
</system.web>

see this answer for more information.

Ali Torabi
  • 32
  • 1
  • 14
0

There are various options to make session active until the user logged out.

And timeout="60" or greater value is not a good way to do it because if you have many active users to your application, it stores user's session for 60 minutes also when user closed the application.

Solution

You can set timeout="10" and do a server hit (timeout value in config - 1) just to tell server your user is still here.

That callback request can be ajax or iframe with a meta tag.

Working Sample:

Put an iframe on your MasterPage or HeaderControl and pass URL to some page in your application and add this meta tag on that page or control

<meta id="MetaRefresh" 
      http-equiv="refresh" 
      content="21600;url=KeepSessionAlive.aspx" 
      runat="server" />

OR Ajax Request

setInterval(function () {
            if (localStorage.getItem("LastSessionHit") == '' || (((new Date - new Date(localStorage.getItem("LastSessionHit"))) / 1000 / 60) >= 9)) {
                $.ajax({ url: "KeepSessionAlive.aspx", }).done(function () {
                    $('#SessionLastHit').val(new Date);
                    localStorage.setItem("LastSessionHit", $('#SessionLastHit').val());
                    Console.log(err);
                }).error(function (err) {
                    Console.log(err);
                });
            }
        }, 60000);
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Aamir Nakhwa
  • 393
  • 1
  • 12