BgEngine.Infraestructure.Security.CodeFirstCrypto.HashPassword C# (CSharp) Метод

HashPassword() публичный статический Метод

public static HashPassword ( string password ) : string
password string
Результат string
        public static string HashPassword(string password)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            // Produce a version 0 (see comment above) password hash.
            byte[] salt;
            byte[] subkey;
            using (var deriveBytes = new Rfc2898DeriveBytes(password, SALT_SIZE, PBKDF2_ITER_COUNT))
            {
                salt = deriveBytes.Salt;
                subkey = deriveBytes.GetBytes(PBKDF2_SUBKEY_LENGTH);
            }

            byte[] outputBytes = new byte[1 + SALT_SIZE + PBKDF2_SUBKEY_LENGTH];
            Buffer.BlockCopy(salt, 0, outputBytes, 1, SALT_SIZE);
            Buffer.BlockCopy(subkey, 0, outputBytes, 1 + SALT_SIZE, PBKDF2_SUBKEY_LENGTH);
            return Convert.ToBase64String(outputBytes);
        }

Usage Example

 public override bool ResetPasswordWithToken(string token, string newPassword)
 {
     if (string.IsNullOrEmpty(newPassword))
     {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token && Usr.PasswordVerificationTokenExpirationDate > DateTime.Now);
         if (user != null)
         {
             dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
             if (newhashedPassword.Length > 128)
             {
                 throw new ArgumentException("Password too long");
             }
             user.Password                  = newhashedPassword;
             user.PasswordChangedDate       = DateTime.UtcNow;
             user.PasswordVerificationToken = null;
             user.PasswordVerificationTokenExpirationDate = null;
             context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
All Usage Examples Of BgEngine.Infraestructure.Security.CodeFirstCrypto::HashPassword