.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Kill session when user closes the browser

Posted By:      Posted Date: May 22, 2011    Points: 0   Category :ASP.Net


I have a requirement from client that to show the concurrent users.

They are using one username and password at different instances. As we need to track the concurrent users, I have wriiten in Global.asmx file for Session_start and session_End , But still showing a differentt result.


So, we have choosen database and when the user logins successfully , count is tracked in the database. And when user loguts count is decreased.

But, we are facing a problem like when user closes the browser how can we end the session and decrease the count in the database. And if be abruptly when he closes the browser or if session gets timed out count should be decreased.

View Complete Post

More Related Resource Links

MS SQL Server: Disconnect Users From Database - Kill User Session

If you ever wanted to restore your database from a SQL backup file (.bak), but there are still users connected to your database, the backup operation will fail causing the error: Exclusive access could not be obtained because the database is in use.

Automatic expiration of forms authentication when user closes the browser windows without signing ou

Dear all, can u tell me how to automatically sign out a user if he/she closes the browser window without signing out. I'm using Forms Authentication.   Thanks 

I need to log out of the application when user closes the browser of a sharepoint 2007 internet webs


HI techies,

         i would like to place the below functionality.

i placed one app in the sharepoint internet site. that app having login/logout functionality ,I need to log out of the application when user closes the browser or Click on the back button on the Browser.

please advice i need to write any javascript or place any code on masterpage?





Nag: http://nagaraju-sharepoint.blogspot.com. Posting is provided with no warranties.

How to set session timeout and clear session in asp.net C# ? And kill session when close the web app


hi ,

How to set session timeout and clear session in web.config and login.aspx  ? And when we close the web application , the session must be cleared ?

I have use session.abandon as per below but is not working . Pls advise .


        protected void btnLogout_Click(object sender, EventArgs e)



Common session for different browser instance


Hello everyone,

I am working on a website which uses a common database shared by another 2-3 websites.  Now i am having a requirement that whenever a user clicks a link provided on another website and comes to my website and if the user is already logged in to my website then my website should not ask him to login again it should by pass the login page and show the next page.

Here the problem is i am not able to get the session objects in newly opened browser window even if the user is already logged in to my website in another browser window. I am not sure but i thing IE uses two different processes for two different browser instance and because of that i am not getting the session objects required.

Deos anyone have some idea on my problem.

Note: I can not ask the another website vendor to pass anything as a querystring.

limit ASP.NET User Session without blocking production

 If the Web Site is in production and there are hits from one particular place (Say London!) and the after 1000 (or x) requests, user session should not be allowed. So what to do in such situation without affecting current production site?  Please Advice. Thanks!  

Accessing Session State in a User Control

We have a web site that implements a custom SiteMapProvider using a User Control added in the master page. I need to be able to limit the sitemap nodes added depending on the logged in user, that is, certain users should not see certain sitemap nodes. Currently, the login processing code determines if users are in the certain category or role and then sets a value in session state, for example, Session["UserInRoleXXX"] = "Yes"; I tried changing the code in the user control to check the session state, but I got the following error: NullReferenceException ... Object reference not set to an instance of an object." Can session state be accessed in a user control? If so, how? If not, any suggested solutions? Thanks in advance for any help.

how to redirect user to login page if session is null

i have 2 page,1.login.aspx ---  2.welcome.aspxin my login.aspx im storing username into sesssion as below:Session["usernm"] = txtUsername.Text;in my welcome.aspx  pagein page_load event if (Session["usernm"]==null)        {            Response.Redirect("Login.aspx");        }----- here if i manulally paste the url ( http://localhost:4125/Loginado/welcome.aspx) it should go to  login.aspx for user credentialsbut its going directly to welcome.aspx  page.

Will Session_End event will be fired evenif somebody directly closes browser without sign out?

Hi,     In one of my application I m using InProc session state.Can anybody tell me if user closes the browser directly,then Session_End will be called? Regards, Mehul

Display a Web User Control in the Web Browser control of WinForms

Does anyone have an example of how to do this, or can you point me to a good tutorial that explains how to do this? Thanks

Check session timeout and warn user before timeout


I know there are several articles explaining how to warn the user before their session times out, but so far none of them have worked for our scenario, so I'm hoping someone here will be able to help me figure this one out...  Here's what we've got:

Our application uses SQLServer to store session state.  We've got it set to timeout in 60 minutes.  Unfortunately (I fought hard against this, but lost the battle) our application uses popups for several major screens.  So tracking the session timeout on the client side doesn't really work, because I have no idea what window the user is in at any given point.  I've tried the solution where you inject Javascript into the page using the current session timeout on each request, but again, because the user could be actively using the application in a different window, it doesn't work for us.  So I guess I have a couple key questions:

  1. Is there any way to get the timeout for a session without triggering a request to the server, which would just renew the session?
  2. Is there a way to track the session timeout value across multiple windows? 
    I believe there is a limit to the number of "branches" the main window can have in our app.  In other words, the user can only be 2 levels deep from the main/root window, s

How to make sure that user doesn't open same webpage in two different browsers or browser tabs?



We are using asp.net StateServer for asp.net session management. In our project we need to make sure that if a user is already logged in and if he is trying to acess the website from a different browser tab, we need to inform him that he is already logged in in another tab and use it instead of opening a new session. Consider the following scenarios:

1. User is logged on in tab1 and his session is still on, then user tries to logon from another tab, the browser sends the asp.net session_id cookie, In the page init i am checking if the Asp.net session Id cookie is present, if it is there i am checking if the it is a new Session (i.e. if the existing session timed out), if it is a new session then proceed with the user action else it means that the session in tab1 is still active so i am redirecting user to error page with appropriate message.

Now the problem with this approach is that, suppose session in tab1 is expired and user opens tab2 and performs some actions, now he goes back to tab1 and perform any action, i directly need to take him to log out page.

Please let me know how this can be achieved.

logging out users when user opens website in two browsers or two tabs of same browser.



We have the implement the following scenario in our application:

  1. We use windows authentication. User opens our website in tab1 and does some action but with out clicking on save he opens a new tab tab2 and opens the same website again.
  2. Now, if the user session in tab1 is active i need to warn users that the he is already logged on to application in some other browser and go to some log out page. But if the user session in tab1 is timed out then he must be able to continue with the website in tab2 as usual, but if he tries to do anything in tab1 he should go to session expired page.

I tried implementing it in following way.

  1. I have a hidden field in each page which will be set to unique Id using GUID.NewID().
  2. when user requests for a page the following code is executed.
  3. if (Request.Headers["Cookie"] == null)
                    //this means user is requesting hte page for first time.
                    //Generate a new GUID store it in session and also in the hidden variable on page.
                    Session["DefaultPage"] = Guid.NewGuid().ToString();
                    ((TextBox)page.FindControl("hiddenGUID")).Text = Session["DefaultPage"].ToString();
                else if (Reque

User keeps losing Session on Anonymous site?


This code was working before, but something has changed and I don't know what.  I have an anonymous registration page.  On it, I have a CAPTCHA image, with a URL pointing to an ASPX page that is generating the image.  On the Registration page I am generating a 6 digit code to be displayed in the CAPTCHA image, then saving it to a Session variable.  On the CAPTCHA ASPX page, I am grabbing that Session variable and creating the image.  However, the Session is resetting when the CAPTCHA ASPX page is called.  If I debug it, it has a different ID and the variable is gone.

Why is this happening and how can I fix it?  I have Sessions enabled in SharePoint 2007.

Open browser with 2 session tabs from C#



I have a button control where I retreive 2 strings with 2 URL like in the code below.

What I wonder how it is possible to do, is how to open up 1 browser window with 2 Session Tabs where 1 Tab is http://www.google.com and the other http://www.yahoo.com

How can this be possible to do from code behind (C#)?

    protected void Button1_Click(object sender, EventArgs e)
		String URL1 = "http://www.google.com";
		String URL2 = "http://www.yahoo.com";

		//How to open up a browser with two session tabs with each URL in each?


Is it possible to preserve viewstate when the user opens multiple browser tabs?


I want my users to be able to open multiple browser tabs and be able to switch back and forth between them.  If Viewstate is saved for each page, I should be able to do this right?



is session shared within multiple browser windows (not tab)


Hi guys,

Really confused on this... let me explain the scenario.

i have a application on .net 2.0 .

once user is logged in, various session variables are stored for that users profile information (normal stuff). when user open a browser window and after doing some operation, closes the browser window directly.

and then reopen new window and try to access homepage of same application... he is presented with profile of the user which was logged in before (and widnow was shut down)

is this behaviour normal and common across all IE versions?

this behaviour is not consistent.... sometimes, its showing homepage of old user and sometimes its  showing login page (as if session is invalidated)

is there any way to abandon all session if browser window is closed (apart from capturing browser window close event)

hope i get some replies on this...

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend