Domanda

sto modificando un sito di Castello-monorotaia che ho ereditato e ha scoperto che sarebbe utile per visualizzare un elenco di utenti online. Attualmente non ci sono filtri che determinano chi può accedere a quali parti del sito in modo da poter distinguere registrati in sessioni da non loggato sessioni. C'è un modo semplice di ottenere un elenco di sessioni attive in modo da poter poi capire quanti utenti sono collegati?

È stato utile?

Soluzione 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!

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top