0

I am trying to test my code by getting 10 specific users that was created recently or having a specific character. I have based my code to this one. This code is from Dan. Changing passwordFormat from Encrypted to Hashed

void HashAllPasswords()
    {
        var clearProvider = Membership.Providers["SqlProvider_Clear"];
        var hashedProvider = Membership.Providers["SqlProvider_Hashed"];
        int dontCare = 2;
        if (clearProvider == null || hashedProvider == null) return;
                        var passwords = encrypted.GetAllUsers(0, int.MaxValue, out dontCare)
        .Cast<MembershipUser>().Where(u => u.IsLockedOut == false && u.IsApproved == true).OrderByDescending(u => u.CreationDate).ToDictionary(u => u.UserName, u => u.GetPassword());
    
        using (var conn = new SqlConnection(
               ConfigurationManager.ConnectionStrings[0].ConnectionString))
        {
            conn.Open();
            using (var cmd = new SqlCommand(
                   "UPDATE [aspnet_Membership] SET [PasswordFormat]=1", conn))
                cmd.ExecuteNonQuery();
        }
    var count = 0;
        foreach (var entry in passwords)
        {
            var resetPassword = hashedProvider.ResetPassword(entry.Key, null);
            hashedProvider.ChangePassword(entry.Key, resetPassword, entry.Value);
            count++;
        if (count == 2)
            break;
        }
    }

So basically instead of changing all the users I just wanted to change 10 users or maybe 100 depending on the amount that I wanted. Also I am receiving an error in

var records

it says

The error is

Item has already been added. Key in dictionary: 'sample@email.com' Key being added: 'sample@email.com'.

Is there a way to resolve this? Maybe I am using linq in the wrong way.

Kit2020
  • 21
  • 2
  • does it matter which users or just a number of users. also does it need to be able to start with a letter(eg i and do that many users)? – Isaac Morris Dec 10 '20 at 04:18
  • @IsaacMorris its somewhat similar like the "like" in sql. Just want to get the users who have a "test" in their names for example. Or is it possible to get only fix amount of users before I loop them and change passwords. The constraints is basically if if it takes too long and execution timeout happens because it gets all the data. – Kit2020 Dec 10 '20 at 04:30

1 Answers1

0

I think the problem is because you have same user name in your database cause Dictionary can't has same key so create use List. Create a User class first and put the data into it.

About too many data to cause the SQL timeout you need to update the SQL query in GetAllUsers()

About the SQL like you can use Contains

About the count you can use Take()

   public class User
        {
            public string UserName { get; set; }
            public string Password { get; set; }
        }

                var passwords = encrypted.GetAllUsers(0, int.MaxValue, out dontCare)
                    .Cast<MembershipUser>()
                    .Where(u => u.IsLockedOut == false && u.IsApproved == true)
                    .OrderByDescending(u => u.CreationDate)
                    .Select(x => new User
                    {
                        UserName = x.UserName,
                        Password = x.GetPassword()
                    });


                //someting like SQL likie
                passwords.Where(user => user.UserName.Contains("Test")).ToList().ForEach(
                    x =>
                    {
                        //reset passwrod
                    });

                // get count
                passwords.Take(10).ToList().ForEach(
                    x =>
                    {
                        //reset passwrod
                    });
MichaelMao
  • 2,596
  • 2
  • 23
  • 54
  • Hello Michaelmao. Thank you for the response. I tried the code above but I am encountering a connection timeout. I have 300k+ of data in my table. I tried putting "connection timeout = 0 or 9000" to extend it but it is still the same. Do you know whats causing this? Is there a way to fix this sir? – Kit2020 Dec 11 '20 at 15:46
  • Change the ```encrypted.GetAllUsers``` don't get some many data in one query or increase timeout – MichaelMao Dec 14 '20 at 03:44