'验证散列密码时不是有效的 Base64 字符串

'Not a valid Base64 String when verifying hashed password

所以我 运行 在用户登录后进行以下设置:

创建新用户时,密码会得到一个生成的哈希值,并与新生成的 Salt 连接起来。 HashedPassword + Salt 存储在与 Salt 列分开的列中。然后我调用方法 VerifyHashedPassword(string storedHashedPass, String password) storedHashedPass 是存储的散列密码(带有盐),password 是用户在登录时输入的明文密码以及从存储中检索到的盐的串联。

但是当我尝试实现它时,它会抛出 'System.FormatException'

谁能帮我弄清楚我做错了什么?

 public static bool VerifyHashedPassword(string hashPassword, String password)
 {
      return System.Web.Helpers.Crypto.VerifyHashedPassword(hashPassword, password);
 }

 public static string GetSalt()
 {
      var random = new RNGCryptoServiceProvider();

      int max_length = 32;

      byte[] salt = new byte[max_length];

      random.GetNonZeroBytes(salt);

      return Convert.ToBase64String(salt);
  }

 public static string hashPassword(string password)
 {
      return System.Web.Helpers.Crypto.HashPassword(password ?? "");
 }

Base64 格式每个字符存储 6 位。其中,由于字节是 8 位,有时最后需要一些填充。附加一个或两个 = 个字符。 = 没有其他用途。

如果您在联接处连接两个 Base64 字符串,可能会有一些填充。将填充放在 Base64 字符串的中间是无效的。

改为连接字节数组,然后进行编码。

The HashedPassword + Salt is stored in a column

这可能是根本问题。您不需要提供或处理盐。参见 this answer
您不需要 GetSalt() 方法。

您不能简单地连接 2 个 base64 字符串,解码器不知道如何处理。