public async Task <IActionResult> Register([FromBody] Account user) { var body = HttpContext.Request.Body.ToString(); Console.WriteLine(body); if (user == null) { Console.WriteLine("Vãi l luôn đầu cắt moi"); } Console.WriteLine(user.Username); user.RoleId = 2; var registrationResult = await _userService.RegisterAsync(user); if (registrationResult != null) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_jwtSttings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new System.Security.Claims.ClaimsIdentity(new[] { new Claim(JwtRegisteredClaimNames.Sub, registrationResult.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.Email, registrationResult.Email), new Claim("Username", registrationResult.Username), }), Expires = DateTime.Now.AddHours(2), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return(await Task.Run(() => Ok(new AuthenticationResult { Success = true, Token = tokenHandler.WriteToken(token) }))); } else { return(await Task.Run(() => BadRequest(new AuthenticationResult { Errors = new [] { "Can not register right now!", "Try again!", } }))); } }
public async Task <IActionResult> Register([FromBody] Account user) { if (user == null) { Console.WriteLine("Vãi l luôn đầu cắt moi"); } Console.WriteLine(user.Username); user.RoleId = 2; var registrationResult = await _userService.RegisterAsync(user); if (registrationResult.Success) { return(await Task.Run(() => Ok(user))); } return(await Task.Run(() => BadRequest(registrationResult))); }
public async Task <IActionResult> Register([FromBody] Account user) { if (ModelState.IsValid) { user.RoleId = 2; var registrationResult = await _userService.RegisterAsync(user); if (registrationResult.Success) { return(await Task.Run(() => Ok(user))); } } return(await Task.Run(() => BadRequest(new AuthenticationResult { Errors = ModelState.Values.SelectMany(x => x.Errors.Select(err => err.ErrorMessage)), }))); }