public static async Task LogoutAsync(string sessionToken) { Authorization.CheckAuthorization(sessionToken, MethodBase.GetCurrentMethod()); try { using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext()) { SessionToken = Authorization.SystemAuthorizationToken }; var session = sessionCtrl.ExecuteQuery(e => e.SessionToken.Equals(sessionToken)) .ToList() .FirstOrDefault(e => e.IsActive); if (session != null) { session.LogoutTime = DateTime.Now; await sessionCtrl.UpdateAsync(session).ConfigureAwait(false); await sessionCtrl.SaveChangesAsync().ConfigureAwait(false); } } catch (LogicException ex) { System.Diagnostics.Debug.WriteLine($"Error in {MethodBase.GetCurrentMethod().Name}: {ex.Message}"); } }
internal static async Task <LoginSession> QueryAliveSessionAsync(string email, byte[] calculatedHash) { email.CheckArgument(nameof(email)); calculatedHash.CheckArgument(nameof(calculatedHash)); LoginSession result = LoginSessions.FirstOrDefault(e => e.IsActive && e.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase) && e.PasswordHash == calculatedHash); if (result == null) { using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext()) { SessionToken = Authorization.SystemAuthorizationToken, }; var identity = identityCtrl.ExecuteQuery(e => e.State == Contracts.Modules.Common.State.Active && e.AccessFailedCount < 4 && e.Email.ToLower() == email.ToLower() && e.PasswordHash == calculatedHash).FirstOrDefault(); if (identity != null) { using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(identityCtrl); var session = sessionCtrl.ExecuteQuery(e => e.LogoutTime == null && e.IdentityId == identity.Id) .ToList() .FirstOrDefault(e => e.IsActive); if (session != null) { result = new LoginSession(); result.CopyProperties(session); result.Identity = new Identity(); result.Identity.CopyProperties(identity); result.Name = identity.Name; result.Email = identity.Email; result.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false)); result.JsonWebToken = JsonWebToken.GenerateToken(new Claim[] { new Claim(ClaimTypes.Email, identity.Email), }.Union(result.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation)))); LoginSessions.Add(result); } } } return(result); }
internal static async Task <LoginSession> QueryAliveSessionAsync(string sessionToken) { LoginSession result = LoginSessions.FirstOrDefault(ls => ls.IsActive && ls.SessionToken.Equals(sessionToken)); if (result == null) { using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext()) { SessionToken = Authorization.SystemAuthorizationToken }; var session = sessionCtrl.ExecuteQuery(e => e.SessionToken.Equals(sessionToken)) .ToList() .FirstOrDefault(e => e.IsActive); if (session != null) { using var identityCtrl = new Controllers.Persistence.Account.IdentityController(sessionCtrl); var identity = identityCtrl.ExecuteQuery(e => e.Id == session.IdentityId).FirstOrDefault(); if (identity != null) { session.Name = identity.Name; session.Email = identity.Email; session.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false)); session.JsonWebToken = JsonWebToken.GenerateToken(new Claim[] { new Claim(ClaimTypes.Email, identity.Email), new Claim(ClaimTypes.System, nameof(QuickNSmart)), }.Union(session.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation)))); result = new LoginSession(); result.CopyProperties(session); LoginSessions.Add(session); } } } return(result); }
private static void UpdateSession() { while (true) { Task.Run(async() => { try { using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext()) { SessionToken = Authorization.SystemAuthorizationToken, }; bool saveChanges = false; var dbSessions = sessionCtrl.ExecuteQuery(e => e.LogoutTime.HasValue == false).ToList(); var uncheckSessions = LoginSessions.Where(i => dbSessions.Any() == false || dbSessions.Any(e => e.Id != i.Id)); foreach (var dbItem in dbSessions) { var itemUpdate = false; var memItemRemove = false; var memItem = LoginSessions.FirstOrDefault(e => e.Id == dbItem.Id); if (memItem != null && memItem.HasChanged) { itemUpdate = true; memItem.HasChanged = false; dbItem.LastAccess = memItem.LastAccess; } if (dbItem.IsTimeout) { itemUpdate = true; if (memItem != null) { memItemRemove = true; } if (dbItem.LogoutTime.HasValue == false) { dbItem.LogoutTime = DateTime.Now; } } if (itemUpdate) { saveChanges = true; await sessionCtrl.ExecuteUpdateAsync(dbItem).ConfigureAwait(false); } if (memItemRemove) { LoginSessions.Remove(memItem); } } if (saveChanges) { await sessionCtrl.SaveChangesAsync().ConfigureAwait(false); } foreach (var memItem in uncheckSessions) { var dbItem = sessionCtrl.ExecuteQueryById(memItem.Id); if (dbItem != null) { memItem.LastAccess = dbItem.LastAccess; memItem.LogoutTime = dbItem.LogoutTime; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error in {MethodBase.GetCurrentMethod().Name}: {ex.Message}"); } }); Thread.Sleep(UpdateDelay); } }