Exemple #1
0
        public TrainerModel GetTrainerByCredentials(CredentialsModel credentials)
        {
            TrainerModel trainerToCheck = new TrainerModel(db.Trainers.SingleOrDefault(p => p.Username == credentials.Username));

            credentials.Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                              password: credentials.Password,
                                                              salt: Convert.FromBase64String(trainerToCheck.Salt),
                                                              prf: KeyDerivationPrf.HMACSHA1,
                                                              iterationCount: 10000,
                                                              numBytesRequested: 256 / 8));

            if (credentials.Password == trainerToCheck.Password)
            {
                return(trainerToCheck);
            }

            return(null);
        }
        public IActionResult Login(CredentialsModel credentials)
        {
            try
            {
                TrainerModel trainer = authLogic.GetTrainerByCredentials(credentials);

                if (trainer == null)
                {
                    return(Unauthorized("incorrect trainername or password"));
                }

                trainer.JwtToken = jwtHelper.GetJwtToken(trainer.Username);

                trainer.Password = null;

                return(Ok(trainer));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }