1

I'm looking for the most efficient way to remove users with duplicate emails in my asp.net MVC2 website that is using the default membership provider.

We launched this site and did not set unique emails to true, now that I am trying to implement a Forgot Username/Password feature I've come to realize over 100 users have re-registered as they forgot their password and there was no forgot password feature. This is a problem as I need to have the user enter their email to send them their username and password reset email. This fails since multiple users share an email. I wish I had noticed the unique email option in the web.config before launch, would have saved a huge hassle. :(

I would like to delete all these accounts easily without having to do it manually 1 by 1, and I will then contact them and let them know their duplicate account has been created.

Whats the best way to go about doing this? Some users have registered with the same email up to 5 times.

Mr Lahey
  • 656
  • 3
  • 15
  • 31
  • [Find the duplicate values in SQL](http://stackoverflow.com/questions/2594829/finding-duplicate-values-in-a-sql-table) and then delete them that way. – Mike Atlas Jul 05 '12 at 19:12

1 Answers1

3

You could call Membership.GetAllUsers() to get a list of all users.

Then group by MembershipUser.Email, decide which one to keep (for example, keep the account with the most recent LastActivityDate), and delete the others (Membership.DeleteUser).

It would be trivial to write a small program to do all this. Of course you might want to consider whether you should consult your users before deleting their account. E.g. you could send an email telling them that the account will be automatically deleted if they don't reply within some period.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Thanks! That helps a lot. We plan to keep the accounts with the latest login date and email them saying the accounts have been merged. Sadly no approach is ideal and I'm kicking myself for not noticing the unique email true or false. – Mr Lahey Jul 05 '12 at 19:31
  • @Duk, In general I think it's better to have users use their email address to log in. IMHO a common reason for duplicate email addresses is when users have not only forgotten their password, but also forgotten the username they created. – Joe Jul 06 '12 at 06:16