0

私は古い.NETメンバーシップのアプローチでウェブサイトを既存している、今私は何をしたいのか、私はこのLinkを言及している暗号化されたパスワードをC#でsqlハッシャパスワードを使用してプレーンテキストに変換する方法

新しいMVC .NETのアイデンティティアプローチに変換され、また、私はDBが新しいIDデシベルに変換しましたそれはすべて完璧に動作します。

しかし、の問題は、新しいパスワードが以前のパスワード .i.eで動作しないということです。パスワードでログインしようとしているときに、パスワードが間違っているというエラーが表示されます。

編集:ここで追加のコード

アプリケーションユーザーマネージャのコードは

public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
     public ApplicationUserManager(IUserStore<ApplicationUser> store) 
      : base(store) 
     { 
     } 

     //Added this constructor after reading microsoft blog https://docs.microsoft.com/en-us/aspnet/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity. 
     //except this constructor other codes are default which identity provides 
     public ApplicationUserManager() 
      : base(new UserStore<ApplicationUser>(new ApplicationDbContext())) 
     { 
      this.PasswordHasher = new SqlPasswordHasher(); 
     } 

     public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
     { 
      var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
      // Configure validation logic for usernames 
      manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
      { 
       AllowOnlyAlphanumericUserNames = false, 
       RequireUniqueEmail = true 
      }; 

      // Configure validation logic for passwords 
      manager.PasswordValidator = new PasswordValidator 
      { 
       RequiredLength = 6, 
       RequireNonLetterOrDigit = true, 
       RequireDigit = true, 
       RequireLowercase = true, 
       RequireUppercase = true, 
      }; 

      // Configure user lockout defaults 
      manager.UserLockoutEnabledByDefault = true; 
      manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
      manager.MaxFailedAccessAttemptsBeforeLockout = 5; 

      // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user 
      // You can write your own provider and plug it in here. 
      manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> 
      { 
       MessageFormat = "Your security code is {0}" 
      }); 
      manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> 
      { 
       Subject = "Security Code", 
       BodyFormat = "Your security code is {0}" 
      }); 
      manager.EmailService = new EmailService(); 
      manager.SmsService = new SmsService(); 
      var dataProtectionProvider = options.DataProtectionProvider; 
      if (dataProtectionProvider != null) 
      { 
       manager.UserTokenProvider = 
        new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
      } 
      return manager; 
     } 
    } 

であるここに私のSQLパスワード調理人コードがあります。

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

ここでログインの方法は、あなたの助けに感謝アカウントコントローラに

 [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(model); 
      } 
      // This doesn't count login failures towards account lockout 
      // To enable password failures to trigger account lockout, change to shouldLockout: true 
      var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
      switch (result) 
      { 
       case SignInStatus.Success: 
        return RedirectToLocal(returnUrl); 
       case SignInStatus.LockedOut: 
        return View("Lockout"); 
       case SignInStatus.RequiresVerification: 
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
       case SignInStatus.Failure: 
       default: 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
      } 
     } 

です!

+0

に従うようですこれよりも –

+0

@更新された最新の投稿とコードが追加されました。 –

+0

メンバーシップで 'SqlPasswordHasher'も使用しましたか? – Alisson

答えて

0

暗号化されたパスワードをプレーンテキストに変換するための新しい方法を自分の質問に追加しました。

問題は、メンバーシップ・テーブル・パスワードで暗号化された形式で格納されたので、我々は、プレーンtext.whichに暗号化されたパスワードを変換することができます方法を実装する必要がありますが、より多くの情報を提供する必要があり

using Microsoft.AspNet.Identity; 
using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 
using System.Web.Security; 
namespace GS365.MVC.Helpers 
{ 
    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 password = passwordProperties[0]; 
       int passwordformat = Convert.ToInt16(passwordProperties[1]); 
       if (passwordformat == 0 || passwordformat == 1) //Password type :0=Clear and 1=Hashed 
       { 
        string salt = passwordProperties[2]; 
        if (String.Equals(EncryptPassword(providedPassword, passwordformat, salt), password, StringComparison.CurrentCultureIgnoreCase)) 
        { 
         return PasswordVerificationResult.SuccessRehashNeeded; 
        } 
        else 
        { 
         return PasswordVerificationResult.Failed; 
        } 
       } 
       else if (passwordformat == 2)//Password type : 2=Encrypted 
       { 
        SqlMembershipProviderHelper sqlmembershipproviderhelper = new SqlMembershipProviderHelper(); 
        var existingPassword = sqlmembershipproviderhelper.GetClearTextPassword(password); 
        if (String.Equals(existingPassword, providedPassword, StringComparison.CurrentCultureIgnoreCase)) 
        { 
         return PasswordVerificationResult.SuccessRehashNeeded; 
        } 
        else 
        { 
         return PasswordVerificationResult.Failed; 
        } 
       } 
       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); 
     } 
    } 

    public class SqlMembershipProviderHelper : SqlMembershipProvider 
    { 
     /// <summary> 
     /// Used for decrypt password into plain text from encrypted type password 
     /// </summary> 
     /// <param name="encryptedPwd"></param> 
     /// <returns></returns> 
     public string GetClearTextPassword(string encryptedPwd) 
     { 
      byte[] encodedPassword = Convert.FromBase64String(encryptedPwd); 
      byte[] bytes = this.DecryptPassword(encodedPassword); 
      if (bytes == null) 
      { 
       return null; 
      } 
      return Encoding.Unicode.GetString(bytes, 0x10, bytes.Length - 0x10); 
     } 
    } 
} 
関連する問題