public UserDetails Authenticate(string name, string password) { var user = _context.Users.SingleOrDefault((obj) => obj.Name == name); if (!_passwordUtilities.VerifyHashedPassword(user, password)) { return(null); } // return null if user not found if (user == null) { return(null); } // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.UserDetailsId.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); user.Token = tokenHandler.WriteToken(token); // save the token to the databasex _context.SaveChangesAsync(); // remove password before returning user.Password = null; return(user); }
public void AddMessage(ChatMessage message) { _context.Add(message); _context.SaveChangesAsync(); }