簡體   English   中英

將現有用戶從MVC 4 SimpleMembership遷移到MVC 5 ASP.NET Identity

[英]Migrating existing users from MVC 4 SimpleMembership to MVC 5 ASP.NET Identity

我有一個MVC 4站點 ,目前正在實現SimpleMembership 在網站的下一次迭代中,我想升級到MVC 5和ASP.NET Identity 這兩個站點在web.config中具有相同的機器密鑰。 SimpleMembership SQL表有一個Password PaswordSalt列,ASP.NET Identity表有一個PasswordHash列,它似乎是Password + PasswordSalt的組合。

我嘗試從舊網站連接密碼PasswordSlat ,但這不起作用。

我的問題是,

如何將現有用戶的密碼從舊網站遷移到新網站?

根據http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity

應用程序用戶的密碼已加密並存儲在數據庫中。 SQL成員資格中使用的加密算法與新身份系統中的加密算法不同。 要重用舊密碼,我們需要在舊用戶使用SQL成員資格算法登錄時有選擇地解密密碼,同時在Identity中為新用戶使用加密算法。

UserManager類有一個屬性'PasswordHasher',它存儲一個實現'IPasswordHasher'接口的類的實例。 這用於在用戶身份驗證事務期間加密/解密密碼。 在步驟3中定義的UserManager類中,創建一個新類SQLPasswordHasher並復制以下代碼。

因此,您必須使用以下代碼創建新的hasher類:

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);
    }

然后在Identity UserManager類中聲明一個使用此hasher的構造函數,例如:

public UserManager()
        : base(new UserStore<User>(new ApplicationDbContext()))
    {
        this.PasswordHasher = new SQLPasswordHasher();
}

它在以下鏈接中說明: http//kevin-junghans.blogspot.com/2014/02/migrating-existing-website-from.html

  public class SimplePasswordHasher : IPasswordHasher
  {
     public string HashPassword(string password)
     {
        return Crypto.HashPassword(password);
     }
     public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
     {
        if(Crypto.VerifyHashedPassword(hashedPassword, providedPassword))
        return PasswordVerificationResult.Success;
        else return PasswordVerificationResult.Failed;
      }  
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM