public IActionResult Authenticate([FromBody] UserProfileTransfer userParam) { var user = _userService.Authenticate(userParam.Username, userParam.Password); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } 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.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); return(Ok(new { Id = user.Id, Username = user.Username, Token = tokenString })); }
public IActionResult Register([FromBody] UserProfileTransfer userParam) { var user = _mapper.Map <UserProfile>(userParam); try { _userService.CreateUser(user, userParam.Password); return(Ok()); } catch (AppException exception) { return(BadRequest(new { message = exception.Message })); } }
public IActionResult UpdateUser(int id, [FromBody] UserProfileTransfer userParam) { var user = _mapper.Map <UserProfile>(userParam); user.Id = id; try { _userService.UpdateUser(user, userParam.Password); return(Ok()); } catch (AppException ex) { return(BadRequest(new { message = ex.Message })); } }