public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { //var userFromRepo = await _repo.Login(userForLoginDto.UserName.ToLower(), userForLoginDto.Password); var user = await _userManager.FindByNameAsync(userForLoginDto.UserName.ToLower()); var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDto.Password, false); if (result.Succeeded) { UserForListDto appUser = _mapper.Map <UserForListDto>(user); return(Ok(new { token = GenerateJwtToken(user), user = appUser })); } else { return(Unauthorized()); } }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { User userFromRepo = await _repo.Login(userForLoginDto.Username.ToLower(), userForLoginDto.Password); if (userFromRepo == null) { return(Unauthorized()); } Claim[] claims = new[] { new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()), new Claim(ClaimTypes.Name, userFromRepo.Username) }; var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8 .GetBytes(_config.GetSection("AppSettings:Token").Value)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = System.DateTime.Now.AddDays(1), SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); UserForListDto user = _mapper.Map <UserForListDto>(userFromRepo); return(Ok(new { token = tokenHandler.WriteToken(token), user })); }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { User userFromRepo = await _authRepository.LoginAsync(userForLoginDto.Username.ToLower(), userForLoginDto.Password); if (userFromRepo == null) { return(Unauthorized()); } // TODO : abstract this JwT creation logic to a new class var claims = new[] { new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()), new Claim(ClaimTypes.Name, userFromRepo.Username) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration.GetSection("AppSettings:Token").Value)); //Token should be a very long string var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.UtcNow.AddDays(1), //this should be variable SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); string tokenStr = tokenHandler.WriteToken(token); UserForListDto user = _mapper.Map <UserForListDto>(userFromRepo); return(Ok(new { tokenStr, user })); }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { // User userFromRepo = await _authRepo.Login(userForLoginDto.Username, userForLoginDto.Password); // if (userFromRepo == null) // return Unauthorized(); User user = await _userManager.FindByNameAsync(userForLoginDto.Username); var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDto.Password, false); if (result.Succeeded) { User appUser = await _userManager.Users.Include(p => p.Photos) .FirstOrDefaultAsync(u => u.NormalizedUserName == userForLoginDto.Username.ToUpper()); UserForListDto userToReturn = _mapper.Map <UserForListDto>(appUser); return(Ok(new { token = CreateAuthToken(appUser).Result, user = userToReturn })); } return(Unauthorized()); }