コード例 #1
0
        public static User Authenticate(Validators.User.UserLogin userLogin)
        {
            using (DatabaseContext db = new DatabaseContext())
            {
                User user = db.Users.SingleOrDefault(x => x.email == userLogin.email);

                //Check if user exists
                if (user == null)
                {
                    return(null);
                }

                //Validate the given password against the user password.
                if (ValidatePassword(userLogin.password, user.password))
                {
                    //If passwords match then return the user.
                    return(user);
                }
                return(null);
            }
        }
コード例 #2
0
        public IActionResult Authorize([FromBody] Validators.User.UserLogin RequestData)
        {
            //Check post data.
            if (ModelState.IsValid == false)
            {
                return(BadRequest("Incorrect post data."));
            }

            //Get autheneticated user.
            User user = UserController.Authenticate(RequestData);

            if (user == null)
            {
                return(BadRequest("Incorrect credentials."));
            }

            //Before generating new tokens for this user, delete all existing tokens.
            this.DeleteAccessTokensForUser(user);

            //Generate tokens for this user.
            return(this.GenerateTokens(user));
        }