I'd like to insert over 1,000 usernames and passwords into the ASP.Identity Users and Memberships table. Ideally, I would like to encrypt the passwords when they are inserted. Can this be done with T-SQL or do I have to use some code in C# to loop through a routine that will encrypt the passwords and insert the usernames and possibly other information? An example of this would be appreciated.
Ok, So I found a way to bulk insert in AspNet Identity Tables Without Performance Hit.
public void CreateBulkIdentityUsers(List<string> userNames)
{
using (ApplicationDbContext context = new ApplicationDbContext())
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var users = context.Users.Where(u => userNames.Contains(u.UserName)).ToList();
PasswordHasher passwordHasher = new PasswordHasher();
foreach (var userName in userNames)
{
var user = users.Where(u => u.UserName == userName).FirstOrDefault();
if (user == null)
{
user = new ApplicationUser()
{
UserName = userName,
EmailConfirmed = false,
PhoneNumberConfirmed = false,
TwoFactorEnabled = false,
LockoutEnabled = false,
PasswordHash = passwordHasher.HashPassword("Password@123"),
SecurityStamp = Guid.NewGuid().ToString().ToLower()
};
context.Users.Add(user);
}
}
context.SaveChanges();
}
}
getting all users before the loop. so that we can check if that user already exists in the database.
initilizing PasswordHasher to hash Password.
Hope, This helps someone. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With