Logout User on Windows close....  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | |
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.
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | re: Logout User on Windows close....
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 ???
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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()?
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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 ???
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | 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
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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.
|  | Expert | | Join Date: Sep 2007 Location: Banglore, India.
Posts: 450
| | | 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.
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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.... -
function HandleClose()
-
{
-
alert("Killing the session on the server!!");
-
PageMethods.AbandonSession();
-
-
}
-
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...
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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 |  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | re: Logout User on Windows close....
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....?
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | re: Logout User on Windows close.... 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....?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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?
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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 ???
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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.
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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 ????
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: Logout User on Windows close....
Only if the function being called is not also in the master page?
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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 ???
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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.
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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....?
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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 ????
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | 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.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | 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. -
function LogOut()
-
{ /* Instanciating the HttpRequest object that will be used to make the Ajax calls.
-
See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
-
var xmlHttp;
-
xmlHttp=GetXmlHttp();
-
/* Logout.aspx is an ASPX page has been created for the sole purpose of logging out the user.*/
-
xmlHttp.open('GET', "http://localhost:1259/Logout.aspx", true);
-
//making the request
-
xmlHttp.send(null);
-
}
-
-
function GetXmlHttp()
-
{ /*This function is responsible for creating an HttpRequest object
-
based on the browser that the user is currently using. */
-
var xmlHttp = null;
-
try
-
{ //Mozilla, Opera, Safari etc.
-
xmlHttp=XMLHttpRequest();
-
}catch(e)
-
{ //Internet Explorer uses ActiveX objects to make Ajax calls.
-
//the following are valid versions, here we will loop through
-
//the versions and attempt to create the ActiveX that matches the browser.
-
var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
-
"Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0",
-
"Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0",
-
"Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
-
for(var i=0; i<versionIds.length && xmlHttp == null; i++)
-
{
-
xmlHttp = CreateXmlHttp(versionIds[i]);
-
}
-
}
-
return xmlHttp;
-
}
-
function CreateXmlHttp(id)
-
{ /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
-
var xmlHttp = null;
-
try
-
{
-
xmlHttp = new ActiveXObject(id);
-
}catch(e) {}
-
return xmlHttp;
-
}
-
Now, you need to set your window.onbeforeunload() event to call this function when the page is about to unload: -
window.onbeforeunload= function(){LogOut();}
-
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...... -
window.onbeforeunload=function()
-
{
-
if(AValidPostback==false){LogOut();}
-
}
-
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 -
myButton.attributes.add("onclick","AValidPostback=true;")
-
Make sure to declare the JavaScript variable "AValidPostback" in your page somewhere.
I hope this makes things clear...
-Frinny
|  | Familiar Sight | | Join Date: Feb 2008
Posts: 211
| | | 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. -
function LogOut()
-
{ /* Instanciating the HttpRequest object that will be used to make the Ajax calls.
-
See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
-
var xmlHttp;
-
xmlHttp=GetXmlHttp();
-
/* Logout.aspx is an ASPX page has been created for the sole purpose of logging out the user.*/
-
xmlHttp.open('GET', "http://localhost:1259/Logout.aspx", true);
-
//making the request
-
xmlHttp.send(null);
-
}
-
-
function GetXmlHttp()
-
{ /*This function is responsible for creating an HttpRequest object
-
based on the browser that the user is currently using. */
-
var xmlHttp = null;
-
try
-
{ //Mozilla, Opera, Safari etc.
-
xmlHttp=XMLHttpRequest();
-
}catch(e)
-
{ //Internet Explorer uses ActiveX objects to make Ajax calls.
-
//the following are valid versions, here we will loop through
-
//the versions and attempt to create the ActiveX that matches the browser.
-
var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
-
"Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0",
-
"Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0",
-
"Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
-
for(var i=0; i<versionIds.length && xmlHttp == null; i++)
-
{
-
xmlHttp = CreateXmlHttp(versionIds[i]);
-
}
-
}
-
return xmlHttp;
-
}
-
function CreateXmlHttp(id)
-
{ /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
-
var xmlHttp = null;
-
try
-
{
-
xmlHttp = new ActiveXObject(id);
-
}catch(e) {}
-
return xmlHttp;
-
}
-
Now, you need to set your window.onbeforeunload() event to call this function when the page is about to unload: -
window.onbeforeunload= function(){LogOut();}
-
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...... -
window.onbeforeunload=function()
-
{
-
if(AValidPostback==false){LogOut();}
-
}
-
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 -
myButton.attributes.add("onclick","AValidPostback=true;")
-
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.....
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|