Ejemplo n.º 1
0
        private async Task <bool> UpdateUserAsync(string username, UpdateDefinition <User> updateDefinition)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(username);
            var updateResult       = await userCollection.UpdateOneAsync(x => x.UserName == normalizedUsername, updateDefinition);

            return(updateResult.IsAcknowledged && updateResult.ModifiedCount == 1);
        }
Ejemplo n.º 2
0
        public async Task <bool> DeleteUserAsync(string username)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(username);
            var collectionPermissionDeletionTask = collectionPermissionsCollection.DeleteManyAsync(x => x.Username == normalizedUsername);
            var userDeletionTask = userCollection.DeleteOneAsync(x => x.UserName == normalizedUsername);
            await Task.WhenAll(collectionPermissionDeletionTask, userDeletionTask);

            return(userDeletionTask.Result.DeletedCount == 1);
        }
Ejemplo n.º 3
0
        public async Task <bool> ChangePasswordAsync(string username, string password)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(username);
            var matchingUser       = await userCollection.Find(x => x.UserName == normalizedUsername).FirstOrDefaultAsync();

            if (matchingUser == null)
            {
                return(false);
            }
            var saltBytes      = Convert.FromBase64String(matchingUser.Salt);
            var passwordHash   = PasswordHasher.Hash(password, saltBytes, PasswordHasher.RecommendedHashLength);
            var passwordBase64 = Convert.ToBase64String(passwordHash);

            return(await UpdateUserAsync(normalizedUsername, Builders <User> .Update.Set(x => x.PasswordHash, passwordBase64)));
        }
Ejemplo n.º 4
0
        public static User Create(RegistrationInformation registrationInformation)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(registrationInformation.Username);
            var salt               = CreateSalt();
            var saltBase64         = Convert.ToBase64String(salt);
            var passwordHash       = PasswordHasher.Hash(registrationInformation.Password, salt, PasswordHasher.RecommendedHashLength);
            var passwordHashBase64 = Convert.ToBase64String(passwordHash);

            return(new User(
                       normalizedUsername,
                       registrationInformation.FirstName,
                       registrationInformation.LastName,
                       registrationInformation.Email,
                       saltBase64,
                       passwordHashBase64,
                       new List <Role>()));
        }
Ejemplo n.º 5
0
        public async Task <User> FindUserAsync(string username)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(username);

            return(await userCollection.Find(x => x.UserName == normalizedUsername).FirstOrDefaultAsync());
        }