コード例 #1
0
        public IActionResult ConnexionEmploye(CompteConnexion CompteConnexion)
        {
            var employe = new Employe();

            try
            {
                if (CompteConnexion == null)
                {
                    throw new ArgumentNullException(nameof(CompteConnexion));
                }


                if (!InputValidationHelper.IsValidUsername(CompteConnexion.NomDeConnexion))
                {
                    throw new ArgumentException(CompteConnexion.NomDeConnexion);
                }

                if (!InputValidationHelper.IsValidPassword(CompteConnexion.MotDePasse))
                {
                    throw new ArgumentException(CompteConnexion.MotDePasse);
                }

                employe        = _db.Employe.FirstOrDefault(e => e.NomUtilisateur == CompteConnexion.NomDeConnexion);
                employe.Compte = _db.Compte.FirstOrDefault(c => c.Id == employe.IdCompte);

                if (employe == null)
                {
                    throw new ArgumentException(CompteConnexion.NomDeConnexion);
                }



                if (!BCrypt.Net.BCrypt.Verify(CompteConnexion.MotDePasse, employe.MotPasse))
                {
                    throw new ArgumentException(CompteConnexion.MotDePasse);
                }


                return(Ok(TokenHelper.CreateToken(employe.Compte.Prenom + " " + employe.Compte.Nom, employe.Compte.Email, employe.NomUtilisateur, "employe")));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest("Requête invalide"));
            }
            catch (ArgumentException)
            {
                return(BadRequest("Informations erronées"));
            }
        }
コード例 #2
0
        public IActionResult Connexion([FromBody] CompteConnexion compteConnexion)
        {
            Compte compte = new Compte();


            try
            {
                if (compteConnexion == null)
                {
                    throw new ArgumentNullException(nameof(compteConnexion));
                }

                if (InputValidationHelper.IsValidEmail(compteConnexion.NomDeConnexion))
                {
                    compte = _db.Compte.Where(c => c.Email == compteConnexion.NomDeConnexion).FirstOrDefault();
                }
                else
                {
                    compte = _db.Compte.Where(c => c.Email == compteConnexion.NomDeConnexion).FirstOrDefault();
                }

                if (string.IsNullOrEmpty(compte.MotPasse))
                {
                    throw new ArgumentNullException(compte.MotPasse);
                }

                if (!PasswordHelper.VerifyPassword(compteConnexion.MotDePasse, compte.MotPasse))
                {
                    throw new ArgumentException(compte.MotPasse);
                }

                return(Ok("allo"));
            }
            catch (ArgumentNullException)
            {
                return(Unauthorized("Informations erronées"));
            }
        }