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

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

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

            byte[] hashedPasswordBytes = Convert.FromBase64String(hashedPassword);

            // Verify a version 0 (see comment above) password hash.

            if (hashedPasswordBytes.Length != (1 + SALT_SIZE + PBKDF2_SUBKEY_LENGTH) || hashedPasswordBytes[0] != (byte)0x00)
            {
                // Wrong length or version header.
                return false;
            }

            byte[] salt = new byte[SALT_SIZE];
            Buffer.BlockCopy(hashedPasswordBytes, 1, salt, 0, SALT_SIZE);
            byte[] storedSubkey = new byte[PBKDF2_SUBKEY_LENGTH];
            Buffer.BlockCopy(hashedPasswordBytes, 1 + SALT_SIZE, storedSubkey, 0, PBKDF2_SUBKEY_LENGTH);

            byte[] generatedSubkey;
            using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, PBKDF2_ITER_COUNT))
            {
                generatedSubkey = deriveBytes.GetBytes(PBKDF2_SUBKEY_LENGTH);
            }
            return ByteArraysEqual(storedSubkey, generatedSubkey);
        }

Usage Example

 public override string ExtendedValidateUser(string userNameOrEmail, string password)
 {
     if (string.IsNullOrEmpty(userNameOrEmail))
     {
         throw CreateArgumentNullOrEmptyException("userNameOrEmail");
     }
     if (string.IsNullOrEmpty(password))
     {
         throw CreateArgumentNullOrEmptyException("password");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         User user = null;
         user = context.Users.FirstOrDefault(Usr => Usr.Username == userNameOrEmail);
         if (user == null)
         {
             user = context.Users.FirstOrDefault(Usr => Usr.Email == userNameOrEmail);
         }
         if (user == null)
         {
             return(string.Empty);
         }
         if (!user.IsConfirmed)
         {
             return(string.Empty);
         }
         dynamic hashedPassword        = user.Password;
         bool    verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, password));
         if (verificationSucceeded)
         {
             user.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int failures = user.PasswordFailuresSinceLastSuccess;
             if (failures != -1)
             {
                 user.PasswordFailuresSinceLastSuccess += 1;
                 user.LastPasswordFailureDate           = DateTime.UtcNow;
             }
         }
         context.SaveChanges();
         if (verificationSucceeded)
         {
             return(user.Username);
         }
         else
         {
             return(string.Empty);
         }
     }
 }
All Usage Examples Of BgEngine.Infraestructure.Security.CodeFirstCrypto::VerifyHashedPassword