Exemplo n.º 1
0
        private void HashPassword(User user)
        {
            FilterDefinition <User> filter;

            // This means we are trying to authenticate the user
            if (user.id == null && user.username != null)
            {
                filter = Builders <User> .Filter.Eq("username", user.username);
            }
            // If we are not authenticating the user, we want to check if the user already exists
            // if not, create a new salt, otherwise use the existing salt
            else
            {
                filter = Builders <User> .Filter.Eq("id", user.id);
            }
            User TempUser = collection.Find(filter).FirstOrDefault();

            if (TempUser == null)
            {
                user.salt = HashService.CreateSalt();
            }
            else
            {
                // It is possible to reach this statement from UpdateUser method
                // the user would still have a salt in the database, but it would not have been passed through JSON, so user.salt would be null
                user.salt = TempUser.salt;
            }
            string PassSaltCombination = user.password + user.salt;

            user.password = HashService.HashString(PassSaltCombination);
        }