Connecting Tech Pros Worldwide Help | Site Map

Logout User on Windows close....

malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#1: May 27 '08
Hi,

I want to implement such a functionality in my project that whenever user is login and if he closes the window without logout then he should be logout and i also wants to change the status in database at a time.. i have tried the onunload and onbeforeunload but it affects only after timeout session period that is after 20 minutes... so how to implement this please give me suggestion... i m waiting.....

thanks in advance...


-malav.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: May 27 '08

re: Logout User on Windows close....


See your previous thread on the exact same matter:
http://bytes.com/forum/thread794330.html
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#3: May 28 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

See your previous thread on the exact same matter:
http://bytes.com/forum/thread794330.html



HI plater,
I know that i have posted same thing before but still my problem is not solved, i also tried onbeforeunload but still the status changes only after session timeout period that is after 20 minutes... so what is the solution of that ???
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: May 28 '08

re: Logout User on Windows close....


Well what are you doing with your ununload function?
Are you sending a message back to your server telling them to Session.Abandon()?
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#5: May 30 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

Well what are you doing with your ununload function?
Are you sending a message back to your server telling them to Session.Abandon()?


HI,
I m calls one javascript function "onunload" event.. and from that i calls "PageMethods.AbandonSession();" and i have write code in global.asax in session end but the code always executes after session timeout that is after 20 minutes so what is the problem ???
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#6: May 30 '08

re: Logout User on Windows close....


Quote:

Originally Posted by malav123

HI,
I m calls one javascript function "onunload" event.. and from that i calls "PageMethods.AbandonSession();" and i have write code in global.asax in session end but the code always executes after session timeout that is after 20 minutes so what is the problem ???

You can't use the "onunload" event because then the page is completely unloaded and it can no longer send anything to the server.

You have to use the "onbeforeunload" event to make an Ajax call to the server that will log out the user.

I explained this in your other thread...do you understand what I'm trying to say?

-Frinny
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#7: Jun 2 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Frinavale

You can't use the "onunload" event because then the page is completely unloaded and it can no longer send anything to the server.

You have to use the "onbeforeunload" event to make an Ajax call to the server that will log out the user.

I explained this in your other thread...do you understand what I'm trying to say?

-Frinny


Hi Frinny,

I also used "onbeforeunload" but still my problem is there as unsolved... i know you have give me suggestion on same related post but still at a time i don't get status change... so what is solution ??? i want to implement this functionality in my project... so please suggest me... waiting for your response....


-malav.
CyberSoftHari's Avatar
Expert
 
Join Date: Sep 2007
Location: Banglore, India.
Posts: 450
#8: Jun 2 '08

re: Logout User on Windows close....


Quote:

Originally Posted by malav123

... so what is solution ??? ...
-malav.

Do not let us assume. It will be helpful, only you post what you had tried so for.
Note : Do not post all code in a class, Just post that method.
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#9: Jun 2 '08

re: Logout User on Windows close....


Quote:

Originally Posted by CyberSoftHari

Do not let us assume. It will be helpful, only you post what you had tried so for.
Note : Do not post all code in a class, Just post that method.


Hi,
In my master page, i calls following javascript function on "onbeforeunload" event....
Expand|Select|Wrap|Line Numbers
  1. function HandleClose()  
  2.     {
  3.          alert("Killing the session on the server!!");
  4.          PageMethods.AbandonSession();
  5.  
  6.     }
  7.  
and i have written sql statement in global.asax to change the status... but it changes after 20 minutes that is the session timeout period...
CyberSoftHari's Avatar
Expert
 
Join Date: Sep 2007
Location: Banglore, India.
Posts: 450
#10: Jun 2 '08

re: Logout User on Windows close....


Hope I had given a reasonable page. Go through this Auto Logout when the application browser closed? thread and try.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#11: Jun 2 '08

re: Logout User on Windows close....


And for PageMethods.AbandonSession(); you have setup the correct ajax functionality to perform this? (I had to look that up, found this: http://aspalliance.com/1294_CodeSnip...e_ServerSide.3
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#12: Jun 2 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

And for PageMethods.AbandonSession(); you have setup the correct ajax functionality to perform this? (I had to look that up, found this: http://aspalliance.com/1294_CodeSnip...e_ServerSide.3



Hi plater,
I already refer this site... in fact i follows the logic described in this site... but still there is no desired solution... as you told i have to setup the correct ajax functionality to accomplish this.. so what i have to do for that....?
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#13: Jun 6 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

And for PageMethods.AbandonSession(); you have setup the correct ajax functionality to perform this? (I had to look that up, found this: http://aspalliance.com/1294_CodeSnip...e_ServerSide.3

Hi plater,
I already refer this site... in fact i follows the logic described in this site... but still there is no desired solution... as you told i have to setup the correct ajax functionality to accomplish this.. so what i have to do for that....?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#14: Jun 9 '08

re: Logout User on Windows close....


Have you implemented the examples developed on that link? Do they work? Can you modify them to fit your needs?
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#15: Jun 9 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

Have you implemented the examples developed on that link? Do they work? Can you modify them to fit your needs?

Yes i have tried same example given in that link and i also modified it according to my needs... but still i gets status change after 20 minutes... not at a time... so where is the problem ???
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#16: Jun 9 '08

re: Logout User on Windows close....


I am not sure.

For heartbeat stuff you need to:
Set ajax call to be happening every I dunno we'll say 30 seconds.
Set your 20min timeout to like 1 minute.


For onBeforeUnload(or whatever) you need to:
Just have it make one call to a logout page that has a Session.Abandon()


EDIT:
I just did this and it worked completely fine. Triggered the Session_End() right away.
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#17: Jun 11 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

I am not sure.

For heartbeat stuff you need to:
Set ajax call to be happening every I dunno we'll say 30 seconds.
Set your 20min timeout to like 1 minute.


For onBeforeUnload(or whatever) you need to:
Just have it make one call to a logout page that has a Session.Abandon()


EDIT:
I just did this and it worked completely fine. Triggered the Session_End() right away.



Hi plater,

I am writing this onbeforeUnload event in master page... so is there any problem arises due to the master page ????
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#18: Jun 11 '08

re: Logout User on Windows close....


Only if the function being called is not also in the master page?
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#19: Jun 12 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

Only if the function being called is not also in the master page?


Oh i have written it in master page... ok so if i write that function in javascript then will it work ???
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#20: Jun 12 '08

re: Logout User on Windows close....


Quote:

Originally Posted by malav123

Oh i have written it in master page... ok so if i write that function in javascript then will it work ???

Well yes, your onbeforeunload would be a javascript function.
That javascript function has to make a call to your application's logout page.
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#21: Jun 16 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

Well yes, your onbeforeunload would be a javascript function.
That javascript function has to make a call to your application's logout page.


Hi plater,
thanks but how to call the
PageMethods.AbandonSession();
from javascript....?
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#22: Jun 16 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Plater

Well yes, your onbeforeunload would be a javascript function.
That javascript function has to make a call to your application's logout page.

Hi plater,
Same code is working on the .js file, but still the problem is same that i m not getting state changed in database at a time... and another problem is that this method calls even when redirect to another page.... so now what to do ????
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#23: Jun 16 '08

re: Logout User on Windows close....


Have an XmlHttpRequest do a request to your logout.aspx page. And THAT page should call the Session.Abandon() function.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#24: Jun 16 '08

re: Logout User on Windows close....


I'm sorry but I've been extremely busy the last couple of weeks.

You need to create a Logout.aspx page whose sole purpose is to log out the logged in user (in the PageLoad event in the Logout.aspx page call your Session.Abandon method and do whatever else you need to do in order to log out the user....). This aspx can be called by the Ajax in your window.onbeforeunload.

Here is Ajax that will make a request to the Logout.aspx.
Expand|Select|Wrap|Line Numbers
  1.         function LogOut() 
  2.         {   /*  Instanciating the HttpRequest object that will be used to make the Ajax calls.  
  3.                 See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
  4.             var xmlHttp;   
  5.             xmlHttp=GetXmlHttp();
  6.             /*  Logout.aspx is an ASPX page has been created for the sole purpose of logging out the user.*/
  7.             xmlHttp.open('GET', "http://localhost:1259/Logout.aspx", true); 
  8.             //making the request
  9.             xmlHttp.send(null); 
  10.         }
  11.  
  12.         function GetXmlHttp()
  13.         {   /*This function is responsible for creating an HttpRequest object 
  14.                 based on the browser that the user is currently using. */
  15.             var xmlHttp = null;            
  16.             try
  17.             {   //Mozilla, Opera, Safari etc.
  18.                 xmlHttp=XMLHttpRequest();
  19.             }catch(e)
  20.             {   //Internet Explorer uses ActiveX objects to make Ajax calls.
  21.                 //the following are valid versions, here we will loop through 
  22.                 //the versions and attempt to create the ActiveX that matches the browser.
  23.                 var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
  24.                          "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
  25.                          "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
  26.                          "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
  27.                 for(var i=0; i<versionIds.length && xmlHttp == null; i++) 
  28.                 {
  29.                     xmlHttp = CreateXmlHttp(versionIds[i]);
  30.                 }
  31.             }
  32.            return xmlHttp;
  33.         }
  34.         function CreateXmlHttp(id) 
  35.         {   /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
  36.             var xmlHttp = null;
  37.             try 
  38.             {
  39.                 xmlHttp = new ActiveXObject(id);
  40.             }catch(e) {}
  41.             return xmlHttp;
  42.         }
  43.  
Now, you need to set your window.onbeforeunload() event to call this function when the page is about to unload:

Expand|Select|Wrap|Line Numbers
  1.      window.onbeforeunload= function(){LogOut();}
  2.  
As you have already discovered, the window.onbeforeunload event is called every time the page unloads. In other words, this means that if your user causes a page to postback, the page will be sent to the server and unloaded, (then the server does processing and sends back a new page which the web browser renders).

This means that you need to have a JavaScript function that will register all valid postback controls...and you need to check this before you call your Ajax that logs out the user......

Expand|Select|Wrap|Line Numbers
  1.      window.onbeforeunload=function()
  2.      {  
  3.           if(AValidPostback==false){LogOut();}  
  4.       }
  5.  
The AValidPostback is a JavaScript boolean variable that will have to be set to true if the user clicks a control that causes a full page postback...

Therefore every single one of your buttons, links etc have to set this value...


eg
Expand|Select|Wrap|Line Numbers
  1. myButton.attributes.add("onclick","AValidPostback=true;")
  2.  
Make sure to declare the JavaScript variable "AValidPostback" in your page somewhere.

I hope this makes things clear...
-Frinny
malav123's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 211
#25: Jun 18 '08

re: Logout User on Windows close....


Quote:

Originally Posted by Frinavale

I'm sorry but I've been extremely busy the last couple of weeks.

You need to create a Logout.aspx page whose sole purpose is to log out the logged in user (in the PageLoad event in the Logout.aspx page call your Session.Abandon method and do whatever else you need to do in order to log out the user....). This aspx can be called by the Ajax in your window.onbeforeunload.

Here is Ajax that will make a request to the Logout.aspx.

Expand|Select|Wrap|Line Numbers
  1.         function LogOut() 
  2.         {   /*  Instanciating the HttpRequest object that will be used to make the Ajax calls.  
  3.                 See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
  4.             var xmlHttp;   
  5.             xmlHttp=GetXmlHttp();
  6.             /*  Logout.aspx is an ASPX page has been created for the sole purpose of logging out the user.*/
  7.             xmlHttp.open('GET', "http://localhost:1259/Logout.aspx", true); 
  8.             //making the request
  9.             xmlHttp.send(null); 
  10.         }
  11.  
  12.         function GetXmlHttp()
  13.         {   /*This function is responsible for creating an HttpRequest object 
  14.                 based on the browser that the user is currently using. */
  15.             var xmlHttp = null;            
  16.             try
  17.             {   //Mozilla, Opera, Safari etc.
  18.                 xmlHttp=XMLHttpRequest();
  19.             }catch(e)
  20.             {   //Internet Explorer uses ActiveX objects to make Ajax calls.
  21.                 //the following are valid versions, here we will loop through 
  22.                 //the versions and attempt to create the ActiveX that matches the browser.
  23.                 var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
  24.                          "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
  25.                          "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
  26.                          "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
  27.                 for(var i=0; i<versionIds.length && xmlHttp == null; i++) 
  28.                 {
  29.                     xmlHttp = CreateXmlHttp(versionIds[i]);
  30.                 }
  31.             }
  32.            return xmlHttp;
  33.         }
  34.         function CreateXmlHttp(id) 
  35.         {   /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
  36.             var xmlHttp = null;
  37.             try 
  38.             {
  39.                 xmlHttp = new ActiveXObject(id);
  40.             }catch(e) {}
  41.             return xmlHttp;
  42.         }
  43.  
Now, you need to set your window.onbeforeunload() event to call this function when the page is about to unload:

Expand|Select|Wrap|Line Numbers
  1.      window.onbeforeunload= function(){LogOut();}
  2.  
As you have already discovered, the window.onbeforeunload event is called every time the page unloads. In other words, this means that if your user causes a page to postback, the page will be sent to the server and unloaded, (then the server does processing and sends back a new page which the web browser renders).

This means that you need to have a JavaScript function that will register all valid postback controls...and you need to check this before you call your Ajax that logs out the user......

Expand|Select|Wrap|Line Numbers
  1.      window.onbeforeunload=function()
  2.      {  
  3.           if(AValidPostback==false){LogOut();}  
  4.       }
  5.  
The AValidPostback is a JavaScript boolean variable that will have to be set to true if the user clicks a control that causes a full page postback...

Therefore every single one of your buttons, links etc have to set this value...


eg
Expand|Select|Wrap|Line Numbers
  1. myButton.attributes.add("onclick","AValidPostback=true;")
  2.  
Make sure to declare the JavaScript variable "AValidPostback" in your page somewhere.

I hope this makes things clear...
-Frinny




Hi frinny,
Thank u so much... let me do all these things in my project then i will reply you...
thanks again.....
Reply