Solved

asp.net+ session_end

Posted on 2004-09-01
10
1,028 Views
Last Modified: 2008-02-26
i have an asp.net application which will limit for an example 10 users to log in at 1 time ( to control network traffic purpose).
The 11th user who wana log on have to hold on until the 1 of the first 10 users log out, the 11th user then will be able to log on to the website.

So i need to detect number of active users who access my website.
This is my coding for this event:

in global.asax:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
Application("UserCount") = Application("UserCount") + 1
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
              Application("UserCount") = Application("UserCount") - 1
End Sub

In web.config:
 <sessionState
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false"
            timeout="5"
 />

In my logout page:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Session.RemoveAll()
End Sub

My question:
Is the session.RemoveAll() will trigger the session_end in global.asax so that it will minus the Application("UserCount") value?
What i have now is although i have logout will a proper way ( thats mean click on logout and trigger the session.removeall() ), but the session_end is depends on the timeout which is set in web.config.This mean the 11th user need to hold on for 5 minutes after 1 of the first 10 users log out.

Or any of you got any alternate way. Please help. Thanks.
Regards,
leeching

0
Comment
Question by:leeching
  • 3
  • 3
  • 2
  • +2
10 Comments
 

Expert Comment

by:Sirusdv
ID: 11960340
It might be alot easier instead of doing a logout like that just set session experation timeout to 0 ( Session.Timeout = 10 )
0
 
LVL 7

Expert Comment

by:vnvk
ID: 11960591
use application_start and Applciation_end also for the same purpose.
I mean, copy the code in session_start to apllicaton_start and session_end to appliation_end

That might help you.
0
 

Accepted Solution

by:
madhukar_konda earned 125 total points
ID: 11960906
Why dont you use Session.Abandon in logout page?, which automatically fires Session_End.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11979540
I totally agree with madhukar_konda, calling the Session.Abandon method should cancel the current session and raise the Session_End event immediately. However it won't happens even after Session.Abandon is called or reach the time out, unless you have stored something in the Session state and it has completed at least one request.

There's no easy way, I guess, to handle such scenario. It might be better to incorporate two timers using client-side javascript in your pages. When the browser is in idle too long, one of the timer become active and prompts the user that the session is going to expire soon. If there's no response from user the other timer that has longer interval will become active and postback the page automatically to execute server-side code to cancel the current session and redirect to login page.
0
 

Author Comment

by:leeching
ID: 11980785
thanx madhukar_konda, session abandon really event session_end immediately and trigger                 application("usercount") -=1..However, it will trigger Application("Usercount") -=1 again after 5 minus which is the timeout set in web config for the session. So for example three users log in and i will get -3 for my application("usercount") after all the three users log out.
May i know why this happen? Besides that, is it a way to detect double usernames log on, which mean the same username is log on on the different machine on the same time?
thanx...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:madhukar_konda
ID: 11993827
Sorry for delay in replying back. We got a long weekend. So delay in replying.

Yes it is possible to check same user logging in from different locations.

There are many ways to check this. One of the popular way is, making a flag when any user logs in. When ever a user logs in to the system, make a flag in the database table. But it purely depends on your situation. Some applications may allow same user logging in at the same time in different systems.

Other way is, prepare a temporary table, log all the users while logging in. Log login in time and logout time. Based on logout time you can find out whether the same user logged in at present or not.

If you can tell me your exact situation, will suggest a better way.

Thanks
Konda
0
 

Author Comment

by:leeching
ID: 11994364
i cant get the user logout time if the user idle for 5 minutes ( timeout set in web config ) or do not logout properly ( without clicking the logout button) .
The session value will be set to nothing.

Besides that, how about my problem regarding session.abandon and application("usercount").
I do alot of research but still cant get a proper way to solve it.
Please help.
0
 
LVL 7

Expert Comment

by:vnvk
ID: 11994414
better use tables in conjunction with session.
@madhukar_konda is right in approach for temp tables or table where u can store session details.

You can write code to fire sessio_abandon when all the window instances are clsoed. get rid of application("usercount"). Instead, store the session details in a temp table an wheever session_abandon or logout takes palce, update the details.

To check the number of logins, retrieve the count from DB where logouttime is null.

Using DB would help you u out for all ur issues
0
 

Expert Comment

by:madhukar_konda
ID: 12003848
What happend leeching? Any luck?
0
 

Author Comment

by:leeching
ID: 12013149
Can u all do me a favour?
May i know iziit after the session_end at global.asax, it will run back to session_start after the timeout period?
Because i found that although im at my logout page, it will loop back to session_start after the timeout period (which is 5 minutes later) by using breakpoint during debugging.
May be i still not familiar with .net. So still can't catch the way .net run.
Because of this, i get negative value for my application("UserCount") because it loop back to session start then to  session end and minus my application("UserCount") again after i logout and idle for 5 minutes(timeout) without closing the browser.
Thanx.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now