public async Task <IActionResult> Login([FromBody] LoginCredentialsDTO login) { //await _userService.CreateUser(new Business.Entities.User { Email = "*****@*****.**", Name = "Harshika Gupta", RoleID = 1, UserName = "******", Password = "******" }); var result = await _userService.AuthenticateUser(login.Email, login.Password); if (result.IsSuccessed) { var user = result.Value; var userToken = new UserToken { Id = user.UserID, FullName = user.Name, RoleID = user.RoleID }; var token = _tokenProvider.CreateToken(userToken); if (!token.IsSuccessed) { BadRequest(token.GetErrorString()); } return(Ok(new AuthTokenDTO { AuthToken = token.Value.Value, RoleName = Utilities.GetRoleName(user.RoleID), ValidTo = token.Value.ValidTo })); } return(BadRequest("User is not authenticated")); }
private static bool CheckIfPasswordMatches(LoginCredentialsDTO credentials, ICustomer customer) { if (customer != null && customer.Registration( ) != null) { return(customer.Registration( ).Password( ).Equals(credentials.Password)); } return(false); }
public ActionResult <LoginResultDTO> Login(LoginCredentialsDTO credentialsDTO) { try { var loginResultDTO = _authService.Login(credentialsDTO); return(Ok(loginResultDTO)); } catch (Exception) { return(Unauthorized("Invalid credentials")); } }
public DisplayResponseLineDTO AuthenticateUserUsing(LoginCredentialsDTO credentials) { if (CheckIfPasswordMatches(credentials, _customers.FindBy(credentials.Username))) { _gateway.AddAuthenticationCookieFor(credentials.Username, _customers.FindBy(credentials.Username).ID( )); return(ValidCredentialsMessage(credentials.Username)); } else { return(InvalidCredentialsMessage( )); } }
public void Should_leverage_task_to_check_if_credentials_are_correct() { LoginCredentialsDTO credentials = ObjectMother.LoginCredentialsDTO( ); using (mockery.Record( )) { SetupResult.For(mockMapper.MapFrom(mockRequest)).Return(credentials); Expect.Call(mockTask.AuthenticateUserUsing(credentials)).Return(null); } using (mockery.Playback( )) { CreateSUT( ).Login( ); } }
public LoginResultDTO Login(LoginCredentialsDTO credentialsDTO) { var user = _userRepository.GetByEmail(credentialsDTO.Email); Validate.NotNull(user); var encryptedPassword = this.GenerateEncryptedPassword(credentialsDTO.Password); Validate.EqualThan(user.Password, encryptedPassword); var token = JwtHandler.GenerateToken(_tokenOptions.Value.SecretKey, user.Name, user.Id, user.IsAdmin); return(new LoginResultDTO(token)); }
public CredentialsDTO GetToken([FromBody] LoginCredentialsDTO login) { CredentialsDTO dto = null; var user = userService.GetByLogin(login); if (user != null) { dto = new CredentialsDTO() { UserId = user.Id.ToString(), LastAccess = user.LastAccess, Login = user.Login, Token = tokenGenerator.Generate(Guid.Parse(user.Id)) }; } return(dto); }
public UserDTO GetByLogin(LoginCredentialsDTO login) { UserDTO dto = null; var user = repository.GetByLogin(login.Login, login.Password); if (user != null) { dto = new UserDTO() { Login = user.Login, LastAccess = user.LastAccess, Id = user.Id.ToString() }; } return(dto); }
public ActionResult <TokenDTO> CreateToken(LoginCredentialsDTO loginCredentials, [FromServices] UsersService service, [FromServices] IOptions <AuthenticationService.Config> config) { var result = service.VerifyUserCredentials(loginCredentials.CreateCommand()); ActionResult <TokenDTO> response; if (result.IsAuthenticated) { var tokenString = AuthenticationService.BuildToken(result.UserCredentials !, config.Value); response = Ok(new TokenDTO { Token = tokenString }); } else { response = Unauthorized(); } return(response); }
public async Task TokenForCorrectCredentialIsCreated(string userName, string password, long userId, UserRole userRole) { var payload = new LoginCredentialsDTO() { Email = userName, Password = password }; var response = await client.PostAsync(EndpointName, payload); AssertExt.EnsureSuccessStatusCode(response); var responseObject = response.GetContent <TokenDTO>().Value; var handler = new JwtSecurityTokenHandler(); var token = handler.ReadToken(responseObject.Token) as JwtSecurityToken; var userIdFromToken = token.Claims.First(claim => claim.Type == ClaimTypes.NameIdentifier).Value; var userRoleFromToken = token.Claims.First(claim => claim.Type == ClaimTypes.Role).Value; Assert.AreEqual(userId.ToString(), userIdFromToken); Assert.AreEqual(userRole.ToString(), userRoleFromToken); }
public DisplayResponseLineDTO AuthenticateUserUsing(LoginCredentialsDTO credentials) { return(realTask.AuthenticateUserUsing(credentials)); }