asp.net+ session_end

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

leechingAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
madhukar_kondaConnect With a Mentor Commented:
Why dont you use Session.Abandon in logout page?, which automatically fires Session_End.
0
 
SirusdvCommented:
It might be alot easier instead of doing a logout like that just set session experation timeout to 0 ( Session.Timeout = 10 )
0
 
vnvkCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ihenryCommented:
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
 
leechingAuthor Commented:
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
 
madhukar_kondaCommented:
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
 
leechingAuthor Commented:
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
 
vnvkCommented:
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
 
madhukar_kondaCommented:
What happend leeching? Any luck?
0
 
leechingAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.