I wanted to stop multiple login of the same user. So, I created a table which keeps track of users who have logged in. When they log in, the data will be entered in the table. When they click on logout, data and session will be removed.
The problem is, when they close the browser window without logging out, they won't be able to login ever again.
Is there a better way to handle this?
You have to delete inforamtion in table on global event Session_End
in global.asax.cs
file
protected void Session_End(Object sender, EventArgs e)
{
//delete from database where userID = Session["userID"]
}
If we are speaking about 'StateServer or SQLServer' session mode than:
Session_End
event will fire once you call Session.Abandon()
. There is job ASPState_Job_DeleteExpiredSessions
on sql server, that expire user sessions if user not logged out using logout button. This job call DeleteExpiredSessions
stored procedure, so i suppose you should place some logic(remove logged user) into this stored procedure.
from documentation:
You can handle the Session_OnEnd event by adding a subroutine named Session_OnEnd to the Global.asax file. The Session_OnEnd subroutine is run when the Abandon method has been called or when the session has expired. A session expires when the number of minutes specified by the Timeout property passes without a request being made for the session.
The Session_OnEnd event is supported only when the session state Mode property is set to InProc, which is the default. If the session state Mode is StateServer or SQLServer, then the Session_OnEnd event in the Global.asax file is ignored. If the session state Mode is set to Custom, then support for the Session_OnEnd event is determined by the custom session-state store provider.
You can use the Session_OnEnd event to clean up session-related information such as information for a user that is tracked in a data source by the SessionID value.
Keap you session time out short and when session is diposed log user out automaticly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With