|
I’ve been searching around internet about how you can control this issue :
You have a Web Application, where you validate that every user that logs in, if somebody try to log from another diferent place with the same user name, the access will be denied because the user is at the system right that moment.
Well, the way that i’m controlling this (as many people that i think are reading this) is using a table where i put all the users’ info. I’ll make this simple. Ex.
Table : employee
-------------------
username
password
active
notice that if the user logs in the application , the field “active” will turn 1… if the user logs off the field will turn 0.
Well ... i have this web application where i have a button to sign out… when i press the button, it invokes a function to update my database to turn “active” =0
but the problem is
what if i press the X button of the navigator? I mean , there’s no way to detect by C# how to catch when i close the browser in order to update the database, right? Everybody told me that the event onUnload (C#) works to detect it… wrong! This event doesnt trigger when i close the window but it does when i browse another web page. So it doesnt help me. Other people told me that Global.Asax catches a window closing with Session_End.. Wrong! Global Asax only works when you tell the aplication to end or the session expires … Believe me.. i tried a lot if things…
Well i guess i found the solution for this problem… i took a long time searching at internet lookin for something until i got this link : http://www.experts-exchange.com/Web/WebDevSoftware/Q_20774799.html ,
and based the answer at that link i decided to do this :
I have Login.aspx, RETURN.aspx (it haves 3 pages, too, left and principal, its a frames page…) and the spyWin.aspx , (special thanks to PE_CF_DEV from experts-exchange for the code, I just took your code and mix it with C#.)
Work with two Session variables, one to save the user login (Session[“USER”]) and another to check if its moment to update the database (Session[“RETURN”]).
1) At your login page … login.aspx, you have to put this in the LOAD event:
if ( Convert.ToString(Session["USER"]).Length > 0 )
{
if (Session["RETURN"]!=null)
{
if ( Convert.ToInt32(Session["RETURN"]) == 1)
{
Session["RETURN"] = 0;
…<Code to reset database>…
}
else
{
Response.Redirect("RETURN.aspx",false);
}
}
}
This will check first if I logged on recently, if the session is still alive and is trying to enter at Login.aspx, it will redirect to RETURN.aspx
2) Put this code at pressed event of the button to log out (this button is located at the left frame) :
Session.Abandon();
Response.Write("<script>javascript: parent.opener=''; parent.close();</script>");
The first line closes session, this is catched by the event Session_end of GLOBAL.ASAX
protected void Session_End(Object sender, EventArgs e)
{
if ( Convert.ToString(Session["USER"]).Length > 0 )
{
<… Your code to update database>
Session["USER"] = null;
}
}
The second one, invokes a javascript event to close the window… usually the browser when detects when you close it, it sends you a question message (message “do you want to close this window?”) .. but next point will solve this and no message window will appear.
3) Put this in the LOAD event of RETURN.aspx ( Not the pages that forms the frames page , i mean , not the left, top, neither the principal page.)
if (Session["USER"]==null)
{
Response.Redirect("Login.aspx",false);
}
else { Session["RETURN"] = 0; }
This will validate if theres a user still with an active session. I’ll explain a little later the use of Session["RETURN"].
I can only say that at this time this session variable is initializated.
4) Put this at the HTML section (in <HEAD> ) of Index.aspx ( Not the pages that forms the frames page , i mean , not the left, top, neither the principal page.)
<script language=javascript>
function launch_spyWin()
{
spyWin = open('spyWin.aspx','spyWin','width=100,height=100,left=2000,top=0,status=0');
spyWin.blur();
}
onunload = launch_spyWin;
</script>
This will solve the point explained the 2nd line exposed at 2). My code is based on code from the user PE_CF_DEV at the link that i mentioned before.
5) With JAVASCRIPT we know that the event “onUnload” will run the function regardless of the page being closed or the user navigating to another page. Ok. Now write this in the LOAD event of spyWin.aspx
if (Convert.ToInt32(Session["RETURN"])==0)
{
Session["RETURN"] = 1 ;
string script = "<script type='text/javascript' language='javascript'> " +
" function check_opener() { " +
" if (opener && opener.closed){ " +
" parent.opener=''; " +
" parent.close(); " +
" window.open('Login.aspx') " +
" } " +
" else{ " +
" parent.opener=''; " +
" parent.close(); " +
" } " +
"} " +
" onload = function() { " +
" self.blur(); " +
" setTimeout('check_opener()',0); " +
" } " +
" </script>";
this.Controls.Add(new LiteralControl(
script));
}
else
{
Session["RETURN"] = 0 ;
}
This spyWindow is an nonvisible popUp window that will trigger when the main window is closed. When i change Session["RETURN"] = 1 now the Login.aspx catches this variable and then update the database (the same function of the Session_End of Global.asax).
Problem Solved! Now my life is easier :P
Hope this helps…
<b></b>
Mario Serrano .
E-SDS Developer
|
|
|
|
|
Hi my friend,
I have the similar problem with session.
Could you send me a template code for this problems.
I'm stuck 2 weeks, please help me. Thank alot.
Please send to :vietnamsimplesoftware@hotmai.com thank alot again.
|
|
|
|