public async Task <IActionResult> CreateUser([FromBody] VmUserCreate vmUserCreate) { if (ModelState.IsValid) { try { User createdUser = await _userService.CreateUser(vmUserCreate); VmUser vmUser = _mapper.Map <VmUser>(createdUser); return(Ok(DataMessage.Data(vmUser))); } catch (AppException ex) { return(BadRequest(ex.ReturnBadRequest())); } catch (Exception ex) { return(BadRequest(AppException.ReturnBadRequest(ex.Message))); } } else { return(BadRequest(AppException.ReturnBadRequest(ModelState))); } }
public async Task <IActionResult> Login([FromBody] VmLoginRequest LoginRequest) { if (ModelState.IsValid) { try { User user = await _userService.ValidateUserToLogin(LoginRequest.UserName, LoginRequest.Password); List <Claim> authClaims = new() { new Claim(ClaimTypes.Name, user.Username), new Claim(JwtRegisteredClaimNames.Jti, user.Id.ToString()), new Claim(ClaimTypes.Role, user.Role.ToString()), }; SymmetricSecurityKey authSigningKey = new(Encoding.UTF8.GetBytes(_config.JWTConfig.Secret)); JwtSecurityToken token = new( issuer : _config.JWTConfig.ValidIssuer, audience : _config.JWTConfig.ValidAudience, expires : DateTime.Now.AddHours(_config.JWTConfig.Expires), claims : authClaims, signingCredentials : new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256) ); string JWTToken = new JwtSecurityTokenHandler().WriteToken(token); return(Ok(DataMessage.Data(new VmLoginResponse() { Token = JWTToken }))); } catch (AppException ex) { return(BadRequest(ex.ReturnBadRequest())); } catch (Exception ex) { return(BadRequest(AppException.ReturnBadRequest(ex.Message))); } } else { return(BadRequest(AppException.ReturnBadRequest(ModelState))); } }