コード例 #1
0
ファイル: UserService.cs プロジェクト: Heiner07/Drive-API
        public async Task <AuthenticatedUser> RegisterUser(RegisterUserRequest request)
        {
            string encryptedPassword = EncrypterUtility.StringToSHA256String(value: request.Password);

            var userToRegister = new UserDrive
            {
                Name     = request.Name,
                Lastname = request.Lastname,
                Username = request.Username,
                Email    = request.Email,
                Password = encryptedPassword,
            };

            _context.UserDrive.Add(userToRegister);
            var entriesWritten = await _context.SaveChangesAsync();

            if (entriesWritten > 0)
            {
                return(await AuthenticateUser(request : new AuthenticationRequest
                {
                    EmailOrUsername = userToRegister.Email,
                    Password = request.Password,
                }
                                              ));
            }

            return(null);
        }
コード例 #2
0
ファイル: UserService.cs プロジェクト: Heiner07/Drive-API
        public async Task <bool> EditUser(int id, EditUserRequest request)
        {
            var userToEdit = await _context.UserDrive.Where(user => user.Id == id).FirstOrDefaultAsync();

            if (userToEdit == null)
            {
                return(false);
            }

            string encryptedPassword = null;

            if (request.NewPassword != null && request.NewPassword.Trim().Length > 0)
            {
                encryptedPassword = EncrypterUtility.StringToSHA256String(value: request.NewPassword);
            }

            userToEdit.Name     = request.Name;
            userToEdit.Lastname = request.Lastname;
            if (encryptedPassword != null)
            {
                userToEdit.Password = encryptedPassword;
            }

            _context.UserDrive.Update(userToEdit);

            var entriesWritten = await _context.SaveChangesAsync();

            if (entriesWritten > 0)
            {
                return(true);
            }

            return(false);
        }
コード例 #3
0
ファイル: UserService.cs プロジェクト: Heiner07/Drive-API
        public async Task <AuthenticatedUser> AuthenticateUser(AuthenticationRequest request)
        {
            string encryptedPassword = EncrypterUtility.StringToSHA256String(value: request.Password);

            // Find a user by the Email or Username
            var userFound = await _context.UserDrive.Where(
                user => (user.Email == request.EmailOrUsername && user.Password == encryptedPassword) ||
                (user.Username == request.EmailOrUsername && user.Password == encryptedPassword)
                ).AsNoTracking().FirstOrDefaultAsync();

            if (userFound == null)
            {
                return(null);
            }

            var authenticatedUser = new AuthenticatedUser
            {
                Email    = userFound.Email,
                Username = userFound.Username,
                Name     = userFound.Name,
                Lastname = userFound.Lastname,
                Token    = GenerateToken(user: userFound),
            };

            return(authenticatedUser);
        }
コード例 #4
0
ファイル: UserService.cs プロジェクト: Heiner07/Drive-API
        public async Task <bool> OldPasswordIsCorrect(int userId, EditUserRequest request)
        {
            string encryptedPassword = EncrypterUtility.StringToSHA256String(value: request.Password);

            var userFound = await _context.UserDrive.Where(user => user.Id == userId)
                            .AsNoTracking().FirstOrDefaultAsync();

            if (userFound == null)
            {
                return(false);
            }

            return(userFound.Password == request.Password);
        }