private string EncodePassword(string password, string salt)
        {
            var encodedPassword = password;

            switch (_passwordFormat)
            {
            case MembershipPasswordFormat.Clear:
                break;

            case MembershipPasswordFormat.Encrypted:
                encodedPassword = Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
                break;

            case MembershipPasswordFormat.Hashed:
                if (string.IsNullOrEmpty(salt))
                {
                    throw new ProviderException("A random salt is required with hashed passwords.");
                }
                encodedPassword = PasswordUtility.HashPassword(password, salt, _hashAlgorithm, _validationKey);
                break;

            default:
                throw new ProviderException("Unsupported password format.");
            }
            return(encodedPassword);
        }
 private void SetUserData(
     string username,
     string password,
     string email,
     string passwordQuestion,
     string passwordAnswer,
     bool isApproved,
     ApplicationUser user)
 {
     user.UserName         = username.Trim();
     user.PasswordSalt     = PasswordUtility.RandomSalt();
     user.PasswordHash     = this.EncodePassword(password.Trim(), user.PasswordSalt);
     user.ContactEmail     = email.Trim();
     user.DateCreated      = DateTime.UtcNow;
     user.PasswordQuestion = string.IsNullOrEmpty(passwordQuestion) ? passwordQuestion : passwordQuestion.Trim();
     user.PasswordAnswer   = string.IsNullOrEmpty(passwordAnswer)
                               ? passwordAnswer
                               : this.EncodePassword(passwordAnswer.Trim(), user.PasswordSalt);
     user.IsApproved  = isApproved;
     user.IsLockedOut = false;
     user.IsOnline    = false;
     user.Enabled     = true;
     if (string.IsNullOrEmpty(user.PrincipalId))
     {
         user.PrincipalId = string.Format("formsauthentication/{0}", Guid.NewGuid());
     }
 }