Skip to content

Instantly share code, notes, and snippets.

@jingyulong
Last active December 18, 2018 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jingyulong/62bb07edf4ac164c0452fac091acbd6f to your computer and use it in GitHub Desktop.
Save jingyulong/62bb07edf4ac164c0452fac091acbd6f to your computer and use it in GitHub Desktop.
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