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.
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.