سؤال

I'm modifying a Castle-Monorail site that I've inherited and found that it would be useful to see a list of currently online users. Currently there are Filters that determine who can access which parts of the site so I can distinguish logged in sessions from non-logged in sessions. Is there an easy way of getting a list of active sessions so that I could then work out who is logged in?

هل كانت مفيدة؟

المحلول 2

Here's the solution I ended up with:

(With help from: https://stackoverflow.com/q/1470571/126785 and Ken Egozi's comments)

In Global.asax.cs:

private static readonly object padlock = new object();
private static Dictionary<string,SessionData> sessions = new Dictionary<string,SessionData>();
public static Dictionary<string, SessionData> Sessions
{
    get { lock (padlock) { return sessions; } }
}

public struct SessionData
{
    public string Name { get; set; }
    public int AccountId { get; set; }
    public string CurrentLocation { get; set; }
}

protected void Session_Start(object sender, EventArgs e)
{
    Sessions.Add(Session.SessionID, new SessionData());
}

protected void Session_End(object sender, EventArgs e)
{
    Sessions.Remove(Session.SessionID);
}

public static void SetSessionData(string sessionId, int accountId, string name, string currentLoc)
{
    Sessions.Remove(sessionId);
    Sessions.Add(sessionId, new SessionData { AccountId = accountId, CurrentLocation = currentLoc, Name = name });
}

public static void SetCurrentLocation(string sessionId, string currentLoc)
{
    SessionData currentData = Sessions[sessionId];
    Sessions.Remove(sessionId);
    Sessions.Add(sessionId, new SessionData { AccountId = currentData.AccountId, CurrentLocation = currentLoc, Name = currentData.Name });
}

Then when logging in:

Global.SetSessionData(((HttpSessionStateContainer)Session.SyncRoot).SessionID,account.Id,account.Name,"Logged In");

Now I just need to work out the best place to update the location from. Calls from each function could be a bit tiresome!

نصائح أخرى

I believe there isnt an easy way, unless you are storing your user logon information in the database, or an application variable, you cannot know how many active sessions there are.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top