public string GenerateToken(string cpf) { try { var user = _context.Users.FirstOrDefault(x => x.CPF == cpf); if (user != null) { var checkTokenExistence = _context.TokenLogs.FirstOrDefault(x => x.UserId == user.Id && x.Active == true); if (checkTokenExistence != null) { checkTokenExistence.Active = false; _context.TokenLogs.Update(checkTokenExistence); _context.SaveChanges(); } var token = MD5Hash(cpf + DateTime.Now.ToString()); TokenLog auth = new TokenLog { Active = true, Token = token, UserId = user.Id, ValidThru = DateTime.Now.AddDays(3), CreationDate = DateTime.Now }; _context.TokenLogs.Add(auth); _context.SaveChanges(); return(token); } else { return(null); } } catch (System.Exception) { throw; } }
//updated token private async Task AddTokenLog(string email, string token, Role role) { var tokenRecord = _context.TokenLogs.SingleOrDefault(i => i.Email == email); if (tokenRecord != null) { tokenRecord.Token = token; _context.TokenLogs.Update(tokenRecord); await _context.SaveChangesAsync(); } TokenLog tokenLog = new TokenLog { Email = email, Token = token, IsAdmin = role == Role.Admin ? true : false }; _context.Add(tokenLog); await _context.SaveChangesAsync(); }