Ejemplo n.º 1
0
        private async Task <bool> AuthenticateEmailAddress(LoginCredentials loginCredentials, CancellationToken token)
        {
            if (_authenticateUserResponse == null)
            {
                _authenticateUserResponse = await _userAuthenticationService.AuthenticateUser(loginCredentials.EmailAddress, loginCredentials.Password);
            }

            return(_authenticateUserResponse.EmailAddressFound);
        }
        public async Task AuthenticateUserWithCorrectNameCorrectPassword()
        {
            //Arrange
            string expectedUsername = "******";
            string password         = "******";

            //Act
            var user = await _userAuthenticationService.AuthenticateUser(expectedUsername,
                                                                         ConvertToSecureString(password));

            //Assert
            Assert.AreEqual(expectedUsername, user.Username);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var authenticationResult = await _authenticationService.AuthenticateUser(model.Username, model.Password);

                if (authenticationResult.IsFailure)
                {
                    ModelState.AddModelError("", authenticationResult.Error);
                }
                else
                {
                    var userIdentity = new ClaimsIdentity(
                        GetUserClaims(authenticationResult.Value),
                        CookieAuthenticationDefaults.AuthenticationScheme);

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(userIdentity),
                        new AuthenticationProperties()
                    {
                        IsPersistent = model.RememberMe
                    });

                    await _dispatcher.Publish(new UserLoggedInEvent(authenticationResult.Value.ID, authenticationResult.Value.Nickname));

                    return(Redirect(model.ReturnUrl));
                }
            }

            return(View(model));
        }
Ejemplo n.º 4
0
        public IActionResult Login([FromBody] LoginCredentials loginCredentials)
        {
            if (loginCredentials == null || !ModelState.IsValid)
            {
                return(Ok(ModelState.Values));
            }
            if (!_userService.CheckEmailExits(loginCredentials.Email))
            {
                return(Ok(new ValidationError {
                    Field = "Email", Message = "Nie istnieje u¿ytkownik z podanym adresem email"
                }));
            }

            var user =
                _authenticationService.AuthenticateUser(loginCredentials.Email, loginCredentials.Password);

            if (user == null)
            {
                return(BadRequest(new ValidationError {
                    Field = "Password", Message = "Nieprawid³owe has³o"
                }));
            }

            if (!user.IsActivated)
            {
                var activationCode = _activationCodeService.CreateNew(user.Email);
                _emailService.SendActivationCode(user.Email, activationCode);
            }

            return(Ok(user));
        }
        public async Task <IActionResult> Authenticate([FromBody] UserInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _context.Users.SingleOrDefaultAsync(x => x.Username == input.Username);

            if (user == null || !_authService.AuthenticateUser(user, input.Password))
            {
                return(Unauthorized());
            }

            _authService.SetOrRefreshAuthenticationToken(user);

            await _context.SaveChangesAsync();

            var model = new UserViewModel()
            {
                Id                  = user.Id,
                Username            = user.Username,
                AuthenticationToken = user.AuthenticationToken
            };

            return(Ok(model));
        }
Ejemplo n.º 6
0
 public IActionResult authenticateuser([FromBody] UserCredentialModel userCredentialModel)
 {
     try
     {
         string msg  = string.Empty;
         var    user = _userAuthenticationService.AuthenticateUser(userCredentialModel, out msg);
         return(AuthenticationResponse(user, msg));
     }
     catch (Exception ex)
     {
         return(BadRequest(new { status = Constants.Error, message = Constants.ErrorMessage, user = "" }));
     }
 }