views:

564

answers:

3

In ASP.NET, each session can be identified by its SessionID variable. Currently, I'm working on a project for which I want to be able to identify each separate user session. In other words, I'm looking for a session identifier or an equivalent variable.

I've looked in the Application, Environment and AppDomain classes, but I couldn't find such a variable. So my question is: how should one identify the session(s) an application is currently handling?

A: 

Maybe System.Diagnostics.Process.GetCurrentProcess().Id would cover your needs? That will give you a number that uniquely identifies the currently running process on the system. The number is valid only while the process runs, and when it has quit any other process may be assigned the same number when it is started.

Fredrik Mörk
Sounds like a step in the right direction. However, I've no guarantee that the next time a user starts an application, the process ID is not identical to the ID of the first start ('session')?
dbaw
No, in theory you can start the application twice in a row and get the same process id. The only guarantee that you have is that on a given system, there will never be two processes running at the same time with the same id. That should not be a problem if you remove session-related info on application close.
Fredrik Mörk
I've looked into System.Diagnostics.Process.GetCurrentProcess() and although there is a property SessionID, the property StartTime largely covers my needs. One can identify a session based on its start time. For the project I'm working one, this will suffice. Thanks.
dbaw
A: 

I'm not quite sure I follow you, but if you're trying to track each instance of the application's lifecycle, you could create a GUID as an instance member somewhere appropriate. Whenever you feel a new "session" has been created, you can create and store this GUID - probably when the user logs in (or the main form loads if you don't have a login mechanism).

I'm assuming of course you have a multi-user enviroment with some kind of server attached, otherwise I can't really see a need for sessions.

lc
A: 

You could check some of the options in the Environment class such as Environment.UserName, Environment.MachineName or Environment.UserDomainName

Hugoware
This is what I currently do. However, I also want to distinguish two application start events from the same user.
dbaw