ASP>NET Identity: How to compare a password entered by the user with a hashed password?
<p>I've created a table called <code>PasswordHistory</code>. Each time a user changes the password, the current password is supposed to be copied to PasswordHistory table. The policy is the most restrictive of the following 2: </p>

<ol>
<li>User cannot use any of the last 8 passwords</li>
<li>or a password that it has used in the last 2 years</li>
</ol>

<p>I'd like to know how to compare a newly entered password with an existing one but that is hashed?</p>

<p>Here's my code:</p>

<pre><code>var _limitDate = DateTime.Now.AddYears(-2);
int n = db.PasswordsHistory.Where(pwd =&gt; pwd.UserId == userId &amp;&amp;  pwd.ChangeDate &gt; _limitDate).Count();

var pwdList = new List&lt;PasswordHistory&gt;();
if(n &gt;= 8)
{
   pwdList = db.PasswordsHistory
       .Where(pwd =&gt; pwd.ChangeDate &gt; _limitDate)
       .ToList();
}
else
{
   pwdList = db.PasswordsHistory
      .OrderByDescending(pwd =&gt; pwd.ChangeDate)
      .Take(8)
      .ToList();
}

if (pwdList.Count == 0)
{
   return false;
}
else
{
   foreach (var pwd in pwdList)
   {
      //compare the password entered by the user with the password stored in the PasswordHistory table
   }
}
</code></pre>

<p>Thanks for helping</p>