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 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)); } }
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)); } }
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); }