public async Task <IActionResult> Post([FromBody] DTOs.UserCredentials credentials)
        {
            Mdls.User user = await userRepository.GetUserByNameOrEmail(credentials.Name);

            if (user != null && user.Active)
            {
                string Password = decryptionProvider.Decrypt(user.Password, user.EncryptionKey);

                if (credentials.Password == Password)
                {
                    Dictionary <string, Func <Mdls.User, object> > contract = new Dictionary <string, Func <Mdls.User, object> >()
                    {
                        { ClaimKeys.USER_ID, (Mdls.User u) => u.id },
                        { ClaimKeys.ROLE, (Mdls.User u) => (int)u.Role }
                    };

                    tokenGenerator.Create(user, contract);
                    string token = tokenProvider.WriteToken <Mdls.User>(tokenGenerator);


                    return(Ok(new { token = token }));
                }
            }



            return(BadRequest(new DTOs.Error("Error On User Credentials")));
        }
Example #2
0
        public ActionResult Authenticate([FromBody] DTOs.UserCredentials userCredentials)
        {
            var userAccount = new stp.data.Repository(_connection).FindUserAccountByUsernameAndPassword(userCredentials.Username, userCredentials.Password);

            if (userAccount == null)
            {
                return(BadRequest(new { message = "Incorrect username and/or password." }));
            }

            return(Ok());
        }
Example #3
0
        public async Task <IActionResult> Authenticate([FromBody] DTOs.UserCredentials userCredentials)
        {
            // TODO: Replace this rudimentary user authentication with feature #161 (https://dev.azure.com/Apptelier/Entrenamiento%20Imaginativo/_workitems/edit/161).
            bool authenticationSucceeded;

            try
            {
                var userAccount = await _mediator.Send(new GetAUserAccountByUsername.Query(userCredentials.Username));

                authenticationSucceeded =
                    userAccount?.PasswordMatches(userCredentials.Password) ?? false;
            }
            catch (Exception e) when(e is ArgumentException || e is ArgumentNullException)
            {
                return(BadRequest(e.Message));
            }
            if (authenticationSucceeded)
            {
                return(Ok());
            }
            return(Unauthorized());
        }