User Migration script from Asp.Membership to Asp identity

Vote:
 

Dear Team,

Recently I implemented owin authentication on my EPi10 project.

If possible please share migration script for usser form asp_membershiptable to AspNetUsers .

I am using the below script 

INSERT INTO AspNetUsers(Id,Email,EmailConfirmed,PasswordHash,SecurityStamp,
PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,
AccessFailedCount,UserName,NewsLetter,IsApproved,IsLockedOut,CreationDate)
SELECT aspnet_Users.UserId,aspnet_Membership.Email,'true',
(aspnet_Membership.Password+'|'+CAST(aspnet_Membership.PasswordFormat as varchar)+'|'+aspnet_Membership.PasswordSalt),
NewID(),NULL,'false','true',aspnet_Membership.LastLockoutDate,'true','0',aspnet_Users.UserName,'false','true','false',isnull(aspnet_Membership.CreateDate,GETDATE())
FROM aspnet_Users
LEFT OUTER JOIN aspnet_Membership ON aspnet_Membership.ApplicationId = aspnet_Users.ApplicationId
AND aspnet_Users.UserId = aspnet_Membership.UserId AND aspnet_Users.UserName

However I am getting the below issue 

Cannot insert duplicate key row in object 'dbo.AspNetUsers' with unique index 'UserNameIndex'. The duplicate key value is (admin).
The statement has been terminated.

Regards

Venkata Phani kumar R

#176965
Edited, Mar 31, 2017 6:59
Vote:
 

Hi,

You can try this one https://docs.microsoft.com/en-us/aspnet/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity

Probably truncate the AspNetUsers before migrating? As alwasy - backup your databases first!

#176981
Mar 31, 2017 13:36
Vote:
 

Hi Quan,

I done th emigration successfully.

however for exsting users when I tried to login I amgetting below excpetion .

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

I have given a try for the below Hash algorithm

 HashAlgorithm hm = HashAlgorithm.Create("HMACSHA512"); 


DO you have any idea which alogithm type do we need to use.

Regards
Venkata Phani Kumar R
#176983
Mar 31, 2017 14:10
Vote:
 

I don't think you have to use any hash algorithm in your code. A normal validate username/password should be enough. I suspect that the values you input into PasswordHash were not in correct format. Can you post one of them here? 

#176986
Mar 31, 2017 14:47
Vote:
 

Hi Quan,

Please find the passwordhash  dp0oE8qpK7mmDpvbi4gkUBTGJFHV+3bZ5UW3+DOd83MS9cjDOT9dsVnYos8eYSjT72yNl9u/UFE5pj0kksCwRg==|1|4HHZsz6ZwJuYF2qyU9sqqA==

Expected password is Isobar123

Regards

Venkata Phani Kumar R

#176988
Mar 31, 2017 14:52
Vote:
 

I think you have an extra space in the beginning, that would be the problem :) 

#176989
Mar 31, 2017 14:53
Vote:
 

Hi Quan,

I have cross verified. no space. 

Even I have removed the passwordHash but no avail. same excpetion. 

Regards

Venkata Phani kumar R

#176991
Mar 31, 2017 15:09
Vote:
 

Did you follow the steps in the link I sent?

public class SQLPasswordHasher : PasswordHasher
{
	public override string HashPassword(string password)
	{
		return base.HashPassword(password);
	}

	public override PasswordVerificationResult VerifyHashedPassword(string  hashedPassword, string providedPassword)
	{
		string[] passwordProperties = hashedPassword.Split('|');
		if (passwordProperties.Length != 3)
		{
			return base.VerifyHashedPassword(hashedPassword, providedPassword);
		}
		else
		{
			string passwordHash = passwordProperties[0];
			int passwordformat = 1;
			string salt = passwordProperties[2];
			if (String.Equals(EncryptPassword(providedPassword, passwordformat, salt), passwordHash, StringComparison.CurrentCultureIgnoreCase))
			{
				return PasswordVerificationResult.SuccessRehashNeeded;
			}
			else
			{
				return PasswordVerificationResult.Failed;
			}
		}
	}

	//This is copied from the existing SQL providers and is provided only for back-compat.
	private string EncryptPassword(string pass, int passwordFormat, string salt)
	{
		if (passwordFormat == 0) // MembershipPasswordFormat.Clear
			return pass;

		byte[] bIn = Encoding.Unicode.GetBytes(pass);
		byte[] bSalt = Convert.FromBase64String(salt);
		byte[] bRet = null;

		if (passwordFormat == 1)
		{ // MembershipPasswordFormat.Hashed 
			HashAlgorithm hm = HashAlgorithm.Create("SHA1");
			if (hm is KeyedHashAlgorithm)
			{
				KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm;
				if (kha.Key.Length == bSalt.Length)
				{
					kha.Key = bSalt;
				}
				else if (kha.Key.Length < bSalt.Length)
				{
					byte[] bKey = new byte[kha.Key.Length];
					Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length);
					kha.Key = bKey;
				}
				else
				{
					byte[] bKey = new byte[kha.Key.Length];
					for (int iter = 0; iter < bKey.Length; )
					{
						int len = Math.Min(bSalt.Length, bKey.Length - iter);
						Buffer.BlockCopy(bSalt, 0, bKey, iter, len);
						iter += len;
					}
					kha.Key = bKey;
				}
				bRet = kha.ComputeHash(bIn);
			}
			else
			{
				byte[] bAll = new byte[bSalt.Length + bIn.Length];
				Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
				Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
				bRet = hm.ComputeHash(bAll);
			}
		}

		return Convert.ToBase64String(bRet);
	}
#176995
Mar 31, 2017 15:15
Vote:
 

Hi Quan,

I have followed the instructions and created the above passwordhasher.

Regards

Venkata Phani Kumar

#177003
Mar 31, 2017 23:28
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.