public async Task <IActionResult> SubmitUser([FromBody] UserViewModel viewModel) { User user = viewModel.GetDbModel(); user.PasswordHash = Sha256Helper.GetHash(viewModel.Password); cateringDbContext.Add(user); await cateringDbContext.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> ChangeUserPassword([FromRoute] int userId, [FromBody] UserPasswordModel model) { User user = await cateringDbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId); string utfPassword = Base64.Base64Decode(model.Password); user.PasswordHash = Sha256Helper.GetHash(utfPassword); cateringDbContext.Update(user); await cateringDbContext.SaveChangesAsync(); return(Ok()); }
public string TryLoginUser(LoginAPIModel model) { model.Password = Base64.Base64Decode(model.Password); User user = cateringDbContext.Users .Include(x => x.Role) .Where(x => x.Email == model.Email || x.Username == model.Email) .Where(x => x.PasswordHash == Sha256Helper.GetHash(model.Password)) .FirstOrDefault(); if (user == null) { return(null); } JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); byte[] key = Encoding.ASCII.GetBytes(appSettings.Secret); var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.UserId.ToString()), new Claim(JwtRegisteredClaimNames.UniqueName, $"{user.FirstName} {user.LastName}"), new Claim(ClaimTypes.Role, user.Role.RoleTitle) }; SigningCredentials signingCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), SigningCredentials = signingCredentials }; SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }