ASP.Net Membership saves changed password as plain text even with Hashed passwordFormat set

前端 未结 3 664
花落未央
花落未央 2021-01-03 12:04

I\'m using the ASP.Net SqlMembershipProvider to manage my users. Here is my config:



        
3条回答
  •  [愿得一人]
    2021-01-03 12:55

    To do that I use a console application. Directly in the database I change PasswordFormat in the aspnet_Membership table. Then I change the password to the same with the help of ResetPassword and ChangePassword methods. Also if the user was locked out I unlock it before changing the password and then lock again. In the app.config file I have connection strings for database model and membership provider, as well as the membership provider definition.

    Note, that if the old password does not satisfies your latest password requirements in the provider settings then the ChangePassword method would fail.

    The code is the following:

    static void Main(string[] args)
    {
        using (var db = new MyEntities())
        {
            var usersToFix = db.aspnet_Membership.Where(x => x.PasswordFormat == 0).ToList();
    
            foreach (var userToFix in usersToFix)
            {
                userToFix.PasswordFormat = 1;
    
                var password = userToFix.Password;
                var passwordQuestion = userToFix.PasswordQuestion;
                var passwordAnswer = userToFix.PasswordAnswer;
                var lastLockoutDate = userToFix.LastLockoutDate;
    
                db.SaveChanges();
    
    
                var user = Membership.GetUser(userToFix.UserId);
                bool locked = user.IsLockedOut;
    
                if (locked)
                {
                    user.UnlockUser();
                }
    
                var resetPassword = user.ResetPassword();
                user.ChangePassword(resetPassword, password);
                user.ChangePasswordQuestionAndAnswer(password, passwordQuestion, passwordAnswer);
    
                if (locked)
                {
                    userToFix.IsLockedOut = true;
                    userToFix.LastLockoutDate = lastLockoutDate;
                    db.SaveChanges();
                }
    
                Console.WriteLine("{0} - OK", user.UserName);
            }
        }
    
    
        Console.WriteLine("Done!");
        Console.ReadKey();
    }
    

提交回复
热议问题