1

我正在修改我继承的 Castle-Monorail 站点,发现查看当前在线用户列表会很有用。目前有过滤器可以确定谁可以访问网站的哪些部分,这样我就可以区分登录会话和未登录会话。是否有一种简单的方法可以获取活动会话列表,以便我可以确定谁已登录?

4

2 回答 2

1

我相信没有简单的方法,除非您将用户登录信息存储在数据库或应用程序变量中,否则您无法知道有多少活动会话。

于 2011-01-28T16:37:28.087 回答
0

这是我最终得到的解决方案:

(借助: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-11-24T15:46:50.697 回答