public async Task Login_ShouldReturnBadRequest_IfUserExistsAndStartUpClass_IsOff() { //arrange var mockRepo = new Mock <IAuthRepository>(); var mockMapper = new Mock <IMapper>(); var mockConf = new Mock <IConfiguration>(); Client c1 = new Client { Id = 1, Username = "******" }; string username = "******"; string password = "******"; ClientForLoginDto cfl = new ClientForLoginDto { Username = "******", Password = "******" }; mockConf.Setup(conf => conf.GetSection("AppSettings:Token")); mockRepo.Setup(repo => repo.Login(username, password)).ReturnsAsync(c1); var authController = new AuthController(mockRepo.Object, mockConf.Object, mockMapper.Object); //failing during creating symmetricKey var res = await authController.Login(cfl); Assert.IsType <BadRequestObjectResult>(res); }
public async Task <IActionResult> Login([FromBody] ClientForLoginDto userForLoginDto) { var userFromRepo = await _repo.Login(userForLoginDto.Email.ToLower(), userForLoginDto.Password); if (userFromRepo == null) { return(Unauthorized()); } // generate token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:Token").Value); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()), new Claim(ClaimTypes.Name, userFromRepo.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 Client Authenticate(ClientForLoginDto clientForLoginDto) { var client = _dbContext.Clients .Include(client => client.Address) .Include(client => client.Occupation) .FirstOrDefault(x => x.Email == clientForLoginDto.Email && x.Password == clientForLoginDto.Password); return(client); }
public IActionResult Authenticate(ClientForLoginDto clientForLoginDto) { var result = _authService.Authenticate(clientForLoginDto); if (result.Success) { return(Ok(result)); } return(BadRequest(new { error = result.Message })); }
public ResponseObject <ClientForGetDto> Authenticate(ClientForLoginDto clientForLoginDto) { var client = _authRepository.Authenticate(clientForLoginDto); if (client == null) { return(new ResponseObject <ClientForGetDto>(false, "Dados incorretos", null)); } return(new ResponseObject <ClientForGetDto>(true, obj: _mapper.Map <ClientForGetDto>(client))); }
public async Task <IActionResult> Login(ClientForLoginDto clientForLoginDto) { var clientFromRepo = await _repo.Login(clientForLoginDto.Username.ToLower(), clientForLoginDto.Password); if (clientFromRepo == null) { return(Unauthorized()); } //Two claims - ClientId, and Client Username var claims = new [] { new Claim(ClaimTypes.NameIdentifier, clientFromRepo.Id.ToString()), new Claim(ClaimTypes.Name, clientFromRepo.Username) }; //Console.WriteLine(claims.ToString()); //create security key var prev = _config.GetSection("AppSettings:Token"); try{ var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value)); //ecrypt key with hashing algorithm var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); //prepare to create the token var tokenDesc = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDesc); var client = _mapper.Map <ClientDetailedDto>(clientFromRepo); //token go back return(Ok(new { token = tokenHandler.WriteToken(token), client })); } catch { return(BadRequest("Cant be reached in testing mode - because of turning off Startup class")); } }
public async Task Login_ShouldReturnUnathorized_IfUsernameNotExist() { // arrange var mockRepo = new Mock <IAuthRepository>(); var mockMapper = new Mock <IMapper>(); var mockConf = new Mock <IConfiguration>(); ClientForLoginDto c1 = new ClientForLoginDto { Username = "******", Password = "******" }; string username = "******"; string password = "******"; //act mockRepo.Setup(rep => rep.UserExists(username)).ReturnsAsync(false); mockRepo.Setup(rep => rep.Login(username, password)).ReturnsAsync((Client)null); var controllerToTest = new AuthController(mockRepo.Object, mockConf.Object, mockMapper.Object); var res = await controllerToTest.Login(c1); //assert Assert.IsType <UnauthorizedResult>(res); }
public async Task <IActionResult> Login(ClientForLoginDto clientForLoginDto) { var clientFromRepo = await _repo.Login(clientForLoginDto.Username, clientForLoginDto.Password); if (clientFromRepo == null) { return(Unauthorized()); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, clientFromRepo.Id.ToString()), new Claim(ClaimTypes.Name, clientFromRepo.Username) }; var key = new SymmetricSecurityKey(Encoding.UTF8 .GetBytes(_config.GetSection("AppSettings:Token").Value)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(Ok(new { token = tokenHandler.WriteToken(token) })); }