예제 #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);
        }
예제 #2
0
 public IActionResult Register(TrainerModel trainer)
 {
     try
     {
         if (authLogic.isTrainerNameExists(trainer.Username))
         {
             return(BadRequest("Username already exists! Choose another username."));
         }
         trainer          = authLogic.Register(trainer);
         trainer.JwtToken = jwtHelper.GetJwtToken(trainer.Username);
         trainer.Password = null;
         return(Created("api/trainers" + trainer.TrainerId, trainer));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }
예제 #3
0
        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));
            }
        }
예제 #4
0
        public TrainerModel Register(TrainerModel trainer)
        {
            trainer.TrainerId = Guid.NewGuid().ToString();
            byte[] salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }
            trainer.Salt     = Convert.ToBase64String(salt);
            trainer.Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                          password: trainer.Password,
                                                          salt: salt,
                                                          prf: KeyDerivationPrf.HMACSHA1,
                                                          iterationCount: 10000,
                                                          numBytesRequested: 256 / 8));

            Trainer trainerToAdd = trainer.ConvertToTrainer();

            db.Trainers.Add(trainerToAdd);
            db.SaveChanges();
            trainer.TrainerId = trainerToAdd.TrainerId;
            return(trainer);
        }