Tech Lover
Tech Lover

Reputation: 111

How to have and manage the Session Timeout in this simple asp.net webforms application?

I am a new ASP.NET Webforms developer and I am struggling right now with how to have and manage session timeout in my simple test application. Generally speaking, the test application is listing a number of items. The user can add whatever he wants to a shopping cart and when he clicks on Checkout, he will be asked to enter his information. There is no login or authentication. However, I am using session variables to pass the user information between different pages.

I need to have a timeout here in such a case that the user leaves the page for a long time. In this case, he should get a message and gets redirected to the home page or any other page.

How to do that?

I tried to do that by adding the following to the web.config file:

<sessionState
    mode="InProc"
    stateConnectionString="tcpip=127.0.0.1:42424"
    stateNetworkTimeout="60"
    sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
    cookieless="false"
    timeout="60"
/>

Upvotes: 2

Views: 2178

Answers (2)

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

First off I'd like to point you to this page here as it seems that:

You are using InProc mode, though providing state server information and timeout which does not make much sense.

There are different modes in this so most probably you only need mode, timeout and cookieless items for InProc (which is the best choice for test applications)

Have you made sure that this is under system.web in web.config?

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

As an extra, your timeout is 60 minutes. Have you waited long enough? If the session is not set anymore you need to handle the redirect yourself.

Here is another SO page that might help you

By the way, if you are to use the in proc mode you can handle session expiry in global.asx

void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends.
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer
    // or SQLServer, the event is not raised.
    Response.Redirect("Add url here");

}

Upvotes: 1

burcu
burcu

Reputation: 526

I was trying to retrieve thousands of records from a web service according to the input parameters and naturally it was trowing timeout exceptions. I decided to put the following code in action to display a client-side message, so the user would change the input parameters to make the results more specific.

You can find this example in here.

Executes any code block:

 public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock)
    {
        try
        {
            Task task = Task.Factory.StartNew(() => codeBlock());
            task.Wait(timeSpan);
            return task.IsCompleted;
        }
        catch (AggregateException ae)
        {
            throw ae.InnerExceptions[0];
        }   
    }

For a specific amount of time:

bool Completed = ExecuteWithTimeLimit(TimeSpan.FromHours(1), () =>
{
     /*your code block*/
});

After stopping the execution, you can redirect to the desired page.

Upvotes: 1

Related Questions