# Password validation not working

**URL:** <https://community.auth0.com/t/password-validation-not-working/43283>\
**Category:** Get Help\
**Tags:** password-hash, database-connections, custom-database, migrate-users\
**Created:** [May 19, 2020, 8:05pm UTC](https://community.auth0.com/t/password-validation-not-working/43283 "2020-05-19T20:05:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![knambiar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/knambiar/32/21390_2.png) [@knambiar](https://community.auth0.com/u/knambiar)\
**Post date:** [May 19, 2020, 8:05pm UTC](https://community.auth0.com/t/password-validation-not-working/43283/1 "2020-05-19T20:05:42Z")

</div>

Hello!

I’m trying to transfer user accounts from our custom Azure SQL database to Auth0 through [automatic migration](https://auth0.com/docs/users/guides/configure-automatic-migration). The custom database uses [ASP.NET](http://ASP.NET) Identity for authentication. I’m using this [login script template](https://auth0.com/docs/connections/database/custom-db/templates/login#asp-net-membership-provider-mvc4-simple-membership-).

```
function fixedTimeComparison(a, b) {
var mismatch = (a.length === b.length ? 0 : 1);
if (mismatch) {
  b = a;
}

for (var i = 0, il = a.length; i < il; ++i) {
  const ac = a.charCodeAt(i);
  const bc = b.charCodeAt(i);
  mismatch += (ac === bc ? 0 : 1);
}

return (mismatch === 0);
}

/**
* validatePassword
*
* This function gets the password entered by the user, and the original password
* hash and salt from database and performs an HMAC SHA256 hash.
*
* @password {[string]} the password entered by the user
* @originalHash {[string]} the original password hashed from the database
* (including the salt).
* @return {[bool]} true if password validates
*/
function validatePassword(password, originalHash, callback) {
const iterations = 1000;
const hashBytes = Buffer.from(originalHash, 'base64');
const salt = hashBytes.slice(1, 17);
const hash = hashBytes.slice(17, 49);
crypto.pbkdf2(password, salt, iterations, hash.length, 'sha1', function(err, hashed) {
  if (err) return callback(err);

  const hashedBase64 = Buffer.from(hashed, 'binary').toString('base64');
  const isValid = fixedTimeComparison(hash.toString('base64'), hashedBase64);

  return callback(null, isValid);
});
}

```

I’ve been getting a 401-Unauthorized error every time I try to log in. It looks like the fixedTimeComparison function returns false. Has anybody run into the same issue?

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [May 20, 2020, 2:25pm UTC](https://community.auth0.com/t/password-validation-not-working/43283/2 "2020-05-20T14:25:34Z")

</div>

You can put in some logging (using the Realtime Webtask Logs extension) and that will give you more information on exactly what is failing.

If that isn’t enough to figure it out, pull the code out into your dev environment, try a known password from the DB and see if you can validate it.

John

---

<div class="post-metadata">

**Author:** ![knambiar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/knambiar/32/21390_2.png) [@knambiar](https://community.auth0.com/u/knambiar)\
**Post date:** [May 21, 2020, 11:43am UTC](https://community.auth0.com/t/password-validation-not-working/43283/3 "2020-05-21T11:43:16Z")

</div>

Hi John!

I got my login script to work. I changed the validatePassword function to work for [ASP.NET](http://ASP.NET) Identity Version 3 password formats.

```
function validatePassword(password, originalHash, callback) {
var iterations = 10000;
var hashBytes = Buffer.from(originalHash, 'base64');
var salt = hashBytes.slice(13, 29);
var hash = hashBytes.slice(29, 61);
crypto.pbkdf2(password, salt, iterations, hash.length, 'sha256', function(err, hashed) 
              {
  							if (err) 
                {        					
    							return callback(err);
  							}
  							var hashedBase64 = Buffer.from(hashed, 'binary').toString('base64');
  							var isValid = fixedTimeComparison(hash.toString('base64'), 
                                                  hashedBase64);

  							return callback(null, isValid);    
							});
}

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 21, 2020, 12:34pm UTC](https://community.auth0.com/t/password-validation-not-working/43283/4 "2020-05-21T12:34:26Z")

</div>

Perfect! Glad to hear that @knambiar!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [June 5, 2020, 12:42pm UTC](https://community.auth0.com/t/password-validation-not-working/43283/5 "2020-06-05T12:42:29Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
