简体   繁体   English

获取Castle-Monorail网站的当前会话?

[英]Get current sessions for Castle-Monorail site?

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. 我正在修改一个继承的Castle-Monorail网站,发现查看当前在线用户的列表将很有用。 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? 是否有一种简单的方法来获取活动会话列表,以便我可以确定谁登录了?

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

Here's the solution I ended up with: 这是我最终得到的解决方案:

(With help from: https://stackoverflow.com/q/1470571/126785 and Ken Egozi's comments) (在以下网站的帮助下: https ://stackoverflow.com/q/1470571/126785和Ken Egozi的评论)

In Global.asax.cs: 在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! 每个函数的调用可能会有些麻烦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM