我正在修改我继承的Castle-Monorail站点,并发现查看当前在线用户的列表会很有用。目前有一些过滤器可以确定谁可以访问站点的哪些部分,这样我就可以区分已登录的会话和未登录的会话。有没有一种简单的方法可以获得活动会话的列表,这样我就可以知道谁已经登录了?
发布于 2011-11-24 23:46:51
这是我最终得到的解决方案:
(来自:https://stackoverflow.com/q/1470571/126785和Ken Egozi的评论)
在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 });
}然后在登录时:
Global.SetSessionData(((HttpSessionStateContainer)Session.SyncRoot).SessionID,account.Id,account.Name,"Logged In");现在我只需要计算出更新位置的最佳位置。从每个函数调用可能有点累人!
发布于 2011-01-29 00:37:28
我相信没有一种简单的方法,除非您将用户登录信息存储在数据库或应用程序变量中,否则您无法知道有多少活动会话。
https://stackoverflow.com/questions/4826446
复制相似问题