I want to log on browser information on a session_start event but, of course in global.asax I can't use
Request.Browser
how should I set it up?
You can use Session_Start in the global.asax file.
protected void Session_Start(Object sender, EventArgs e)
{
HttpContext context = base.Context;
HttpRequest request = context.Request;
HttpBrowserCapabilities browserCapabilities = request.Browser;
// log, for example the browser's name:
string browserName = browserCapabilities.Browser; // Firefox in my case
}
The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.
Working with the ASP.NET Global.asax file
Related
I want to create a brand new ASP.NET_SessionId when the Session Times out on my website. I think the event that fires when your session times out is Session_End in Global.asax file. So I would like to do something in this event
To delete current session, it will fire on Session.Abandon():
Global.asax
public void Session_End(object sender, EventArgs e)
{
string sessionId = this.Session.SessionID;
// Query to delete User's current session
//...
//...
}
So you would redirect the user to the login form to generate a new Session.
In my application, we want to log some activity and messages to the DB when the window is closed by the user or when the session timesout. Is there any tested code to do this? Is writing this code in Session_End method of Global.asax.cs the right way?
You may be able to do something like this for the session (note that there is no guaranteed way to check for a browser closing). This approach will only work if you are using inProc for your session.
In your global.asax.cs
protected void Session_Start(object sender, EventArgs e)
{
//a value must be present to call Session_End so we pre-seed it
Session["SeededValueToEnableSession_End"] = true;
}
protected void Session_End(object sender, EventArgs e)
{
//handle session ending
}
Can I add an event handler to a session value in asp.net with c#?
I'm planning to update the database when user logs in and logs out and I control login and logout with session values, but the session may end without a user click, like timeout, so I'd like to add an event handler to "isloggedin" session value.
you need to handle this in global.asax.
protected void Session_Start(Object sender, EventArgs e)
{
//your code
}
protected void Session_End(Object sender, EventArgs e)
{
//your code
}
You can implement the Session_End method in the global.asax. There, you will retrieve what information you need from the session, and do what you need to (like setting logout flag in database for the user):
Here's an example of what I do, though you can tailor it to your needs:
void Session_End(object sender, EventArgs e)
{
SessionPlayerContext context = (SessionPlayerContext)this.Session[Constants.SessionKeys.UserContext];
if (context != null)
PlayerManager.SetPlayerOnlineStatus(context.PlayerID, false);
}
The key is that I'm getting the user object that I previously stored in the session (when user logged in), and if it exists, I then flag the user as being logged out in the database (via PlayerManager)
Not only should you check it in the Session_End, but you also should check it wherever user would physically log out.
As for setting the user as being logged in, you will handle that when user physically logs in.
I'm programming an MVC3 application. Now I hava to call a script if the users session expire.
Is there something like a event on-session-expire, that get fired when the user session expired?
Thanks a lot!
In your Global.asax
you can create a
protected void Session_End(object sender, EventArgs e) { }
method which should be called when a session ends.
I'm trying to implement a class (lets call it CustomerConnection class) which implements System.Web.HttpApplication interface. I tried to implement two events session_end and application_end events in this class in this way:
public class CustomerConnection: System.Web.HttpApplication
{
protected void Session_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
SqlConnection connection = context.Items[Database_Con] as SqlConnection;
connection.close();
}
protected void Application_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//someotherEvent
}
But it seems these events are not getting executed neither for the application end or session log out.
How should i make these events get´executed ? Is it like i have to notify to certain other event?
I'm just creating a new object of customer class. is it enough to get these events executed or my approach is fundamentally wrong?
Please help me out
Thanks in anticipation
You need this in global.asax:
<%# Application Language="C#" Inherits="CustomerConnection"%>
Caveat, I've never tried this, I found it here: http://www.codedigest.com/Articles/ASPNET/34_Adding_CodeBehind_for_Gloabalasaz_x_file_in_aspnet_20.aspx
The object that you have created isn't connected to anything, so it can't receive any events from the application. You have to put the event handlers in the object that is used for the web application, i.e. in the Global.asax.cs file.
Besides, there is no Session_End event, it's Session_OnEnd.