public async Task CheckIfUserDisconnected(string userId) { var user = Users.FirstOrDefault(k => k.UserId == userId); bool disconnected = true; if (user != null) { // user has no connections disconnected = !user.IsActive; if (disconnected) { // remove from obs users Users.Remove(user); // end log var activeLog = ApplicationContext.ActiveLogs.FirstOrDefault(k => k.UserId == userId); if (activeLog != null) { _userLogsMethods.EndActiveLog(userId, DataContract.UserLogStatus.Disconnected); ApplicationContext.ActiveLogs.Remove(activeLog); await Clients.All.SendAsync("RefreshLogs"); } } } }
public async Task <IActionResult> Logout() { if (!User.Identity.IsAuthenticated) { return(Redirect("/login")); } var user = await _userManager.FindByEmailAsync(User.Identity.Name); if (user == null) { user = await _userManager.FindByNameAsync(User.Identity.Name); } var id = user?.Id; await _signInManager.SignOutAsync(); // delete from active users try { UserLog log = ApplicationContext.ActiveLogs.FirstOrDefault(k => k.UserId == id); if (log != null) { ApplicationContext.ActiveLogs.Remove(log); } } catch (Exception ex) { } // end db log session _userLogsMethods.EndActiveLog(id, UserLogStatus.Logout); await observerHub.Clients.All.SendAsync("RefreshLogs"); return(Redirect("/login")); }