Skip to content

Instantly share code, notes, and snippets.

@jingyulong jingyulong/UniqueLogin.cs
Last active Dec 18, 2018

Embed
What would you like to do?
Unique Login
public class OnlineInfo
{
private static readonly string CurrentSessionId = HttpContext.Current.Session.SessionID;
public static string CurrentUserName { get; set; }
/// <summary>
/// Key:SessionID,Value:UserName
/// </summary>
public static Dictionary<string, string> List
{
get => (Dictionary<string, string>)HttpContext.Current.Application["Online"] ??
new Dictionary<string, string>();
set
{
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["Online"] = value;
HttpContext.Current.Application.UnLock();
}
}
public static bool IsOnline => List.ContainsKey(CurrentSessionId) && List[CurrentSessionId] != "OffLine";
public static bool HasKey => List.ContainsKey(CurrentSessionId);
public static bool HasUser => List.ContainsValue(CurrentUserName);
public static void Login()
{
var token = Authentication.GetToken(CurrentUserName);
HttpContext.Current.Session[HttpContext.Current.Session.SessionID] = token;
if (HasUser)
{
var previousUserSessionKey = List.First(d => d.Value == CurrentUserName).Key;
List[previousUserSessionKey] = "OffLine";
}
if (!HasKey)
{
List.Add(CurrentSessionId, CurrentUserName);
}
}
public static void CheckIsOnline()
{
if (!IsOnline)
{
Logout();
}
}
public static void Logout()
{
HttpContext.Current.Session[CurrentSessionId] = null;
List.Remove(CurrentSessionId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.