?
Solved

Using the Global.asax file

Posted on 2008-06-23
11
Medium Priority
?
1,041 Views
Last Modified: 2010-04-21
Hi,
How do I call a method on one of my .aspx pages from my global.asax page?
For example I have the method Logout() on my default.aspx page.
When the Session_End event fires on the global.asax page, I'd like it to call the Logout() method from the default.aspx page.
0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 21849136
Not so fast Session_End can and will fire without a specific user request hitting the server.  If your goal here is to logout the user, destroy any cookies, etc., you can't do this from this method.
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21849352
Ok, maybe I need a different approach then.  What I basically need is a way to capture if a user has closed the browser or tab containing my website without logging out first, so that I can log the account out.  Would using page unload be a better option?
0
 
LVL 33

Expert Comment

by:raterus
ID: 21849431
When you say log out the user, exactly what do you want to happen?

You can catch the closing of the tab/window by something like this <body onunload="someFunction();">.  However the minute you try to request another page, it going to be popup blocked, guaranteed!  You also will have a very hard time trying to figure out is the the last window they have open to your site?  I can tell you for one, I like to open up a site into multiple tabs, and if I closed one and you logged me out, even though I had two other windows open, I'd be rather unhappy.

You really should revisit how to gracefully clean up / log out a user from the server, and forget their client browser.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21849607
Good Points.

Quote: "When you say log out the user, exactly what do you want to happen?"

My user table on the Database has a column titled "Status".  Currently when a user successfully logs in, an update statement is passed to the database to set the value of Status = online for that specific user.  When that user clicks the logout button to logout, a update statement is passed to the database to set Status = offline.  Ive done this, so that when the site admin has logged in, he/she can check and see how many users are currently logged in, and what the specific names of the users are.  However, under this setup, if a user doesnt properly logout, then the update statement is not being passed to the database to set Status = offline, and the user appears to always be online.

There are probably better ways to go about doing this......
Im definitely open to better ideas, as Im a new, inexperienced programmer - However, Im thinking my method would suffice if I could somehow get an event to trigger when a user exited my website without properly logging off.
0
 
LVL 33

Expert Comment

by:raterus
ID: 21849848
I think you're right on about putting this in Session_End, I just wanted to clarify with you that this isn't tied to a specific request on behalf of the user, so you don't have access to the Request/Response objects.  However you should be able to get the username and run some last minute code to change the status.

As for actually running this code, I'd move the LogOut code to somewhere in App_Code, and have both your logout page and Session_end call the same method.

You also might want to make sure in Application_Start, you set all users to the logged out status, because if your application abruptly restarts (like when you put out a new version), you can have issues there.
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21849911
Hmm, I've never put code into App_Code before, or called upon code from there, but it makes sense and is right along the lines of what I was just thinking.  What I was just about to test was, simply copying the same lines of code I have in my Logout() method and putting them into Session_End().  However, following your advice and putting the code into App_Code would seem to be the correct way to do it, I'm just not sure how.
0
 
LVL 33

Expert Comment

by:raterus
ID: 21849979
You put classes into App_Code, and that gets into the whole discussion of object-oriented programming.  If you post your logout code, I can probably tell you how to best put this in App_Code.
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21849993
When does Application_Start actually run?

Also, in testing I have the code from Logout() now in Session_End(), however, when testing it seems that the users is still logged in.  I'm assuming that this is because the session state is set to a default timeout of 20 min, and I'm am testing too soon, before the timeout takes place.  Does this sound correct?  If this is the case, does the server hold and run the lines of code found in Session_End() when the timeout finally occurs?
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21850012
Logout code below:
protected void Logout(object sender, EventArgs e)
    {
        string privateID = Session["PrivateID"].ToString();
        string statusSQL = "UPDATE [Users] SET [LogStatus] = 'OFF' WHERE [PrivateID] = '" + privateID +"'";
        SqlCommand statusCMD = new SqlCommand(statusSQL, conn);
        conn.Open();
        statusCMD.ExecuteNonQuery();
        conn.Close();
 
        Session.Clear();
        Response.Redirect("http://www.mycompany.com", true);
    }

Open in new window

0
 
LVL 33

Accepted Solution

by:
raterus earned 2000 total points
ID: 21850059
Application_Start runs on the first request to the application

Yes, you'll need to wait at least 20 minutes until you test, and remember, if you refresh too soon, your session timeout will renew, and you'll have to wait even longer.

The only thing you can't have in the Logout code if you put it in app_code is the Response.Redirect at the end.  You can keep this in the Logout page though

I'd use a shared function

public class AppFunctions()
{
  public static void Logout()
  {
        string privateID = Session["PrivateID"].ToString();
        string statusSQL = "UPDATE [Users] SET [LogStatus] = 'OFF' WHERE [PrivateID] = '" + privateID +"'";
        SqlCommand statusCMD = new SqlCommand(statusSQL, conn);
        conn.Open();
        statusCMD.ExecuteNonQuery();
        conn.Close();

  }
}

Then in your logout page you'd use
AppFunctions.Logout();
Response.Redirect("http://www.mycompany.com", true);

and in Session_End you'd simply call
AppFunctions.Logout();
0
 
LVL 10

Author Closing Comment

by:P1ST0LPETE
ID: 31469846
Makes sense, thanks for the help.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question