Exemple #1
0
        public Task <ActionResult <TokenViewModel> > PostAsync([FromBody] TokenInputModel inputModel)
        => ExecuteAsync <TokenViewModel>(async() =>
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            return(Ok(await _tokenService.GenerateTokenAsync(inputModel.Username, inputModel.Password)));
        });
        public async Task <ActionResult <TokenViewModel> > PostAsync([FromBody] TokenInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            try
            {
                var result = await _provider.GenerateTokenAsync(inputModel.Email, inputModel.Password);

                return(Ok(result));
            }
            catch (ArgumentException e)
            {
                return(Unauthorized(e.Message));
            }
        }
        public ActionResult Post([FromBody] TokenInputModel input)
        {
            var jwt = new JWTHelper(_config);

            // Ligne de test pour générer des passwords hashés
            // return Ok(BCrypt.Net.BCrypt.HashPassword(input.Password));

            using (_dal)
            {
                // On récupère l'utilisateur en fonction de son nom
                var user = _dal.Users.FirstOrDefault(u => u.Name == input.Name);
                if (user == null)
                {
                    return(Unauthorized());
                }

                // Si le mot de passe correspond, on récupère un token signé
                if (BCrypt.Net.BCrypt.Verify(input.Password, user.Password))
                {
                    var mainPlayer = _dal.Players.FirstOrDefault(p => p.UserId == user.Id && p.IsCurrentPlayer);
                    if (mainPlayer == null)
                    {
                        mainPlayer = new Player()
                        {
                            Name = string.Empty, UserId = user.Id
                        }
                    }
                    ;

                    var token = jwt.GenerateSecurityToken(mainPlayer);
                    return(Ok(token));
                }
                else
                {
                    return(Unauthorized());
                }
            }
        }