public async Task <ActionResult <UserDto> > Login(LoginDtos loginDtos) { var user = await _context.Users.SingleOrDefaultAsync(user => user.UserName == loginDtos.UserName); if (user == null) { return(Unauthorized("錯誤的帳號")); } using var hmac = new HMACSHA512(user.PasswordSalt); var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDtos.Password)); for (int i = 0; i < computedHash.Length; i++) { if (user.PasswordHash[i] != computedHash[i]) { return(Unauthorized("錯誤的密碼")); } } return(new UserDto { Username = user.UserName, Token = _tokenService.CreateToken(user) }); }
public async Task <ActionResult> SignIn(LoginDtos input) { if (!await authRepository.UserExist(input.Email.ToLower())) { return(BadRequest("Email does't exist")); } var person = await authRepository.SignIn(mapper.Map <Person>(input), input.Password); if (person == null) { return(BadRequest("Incorrect password")); } var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(configuration.GetSection("AppSettings:Token").Value); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, person.Id.ToString()), new Claim(ClaimTypes.Name, person.Email) }), Expires = DateTime.Now.AddDays(1), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); return(Ok(new { tokenString })); }
public async Task <IActionResult> Check([FromBody] LoginDtos model) { var result = await _loginService.Login(model); if (result.Message != ApiResultMessages.Ok) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <ApiResults> Login(LoginDtos model) { var entity = await _context.Login.Where(x => x.Username == model.Username && x.Password == model.Password).FirstOrDefaultAsync(); if (entity == null) { return new ApiResults { Data = model.Username, Message = ApiResultMessages.UNW001 } } ; return(new ApiResults { Data = model.Username, Message = ApiResultMessages.Ok }); } }