public async Task <IActionResult> PlexAuthentication(string username, string password) { var loginResult = await _plexService.Login(username, password); if (loginResult.Succes) { var user = loginResult.User; _plexSessionRepo.DeleteOldSessions(_maxSessionAge); var sessionsQuery = _plexSessionRepo.GetSessionsForEmail(loginResult.User.Email).Where(s => s.Token == user.Token && s.Username == user.Username); PlexAuthentication session; if (sessionsQuery.Any()) { session = sessionsQuery.Single(); // there should really be only one session at this point // all we have to do now is update the time and refresh the thumbnail session.SessionStart = DateTime.Now; session.Thumbnail = loginResult.User.Thumbnail; _plexSessionRepo.Update(session); } else { // this sessions doesn't exist yet, so we'll save it now session = _plexSessionRepo.Save(user.Email, user.Token, user.Username, DateTime.Now, user.Thumbnail); } return(RedirectToAction("Index", new { Succes = true })); } return(RedirectToAction("Index", new { Succes = false, Error = loginResult.Error })); }