Search code examples
asp.net-mvcentity-framework-4asp.net-identity-2

Identity 2 how to allow duplicate Names for logical deletes


How can I get Identity 2 to allow more than one user with the same name?

In my application I don't want to physically delete a user from the database. So I have added a delete flag to the Users table.

Then I dropped the Index that required UserName to be unique. And created a filtered Index IX_USER_DELET_FG_Flase

DropIndex("dbo.USERS", "UserNameIndex ON dbo.USER");    
Sql("CREATE UNIQUE INDEX IX_USER_DELET_FG_Flase ON dbo.USERS (UserName) WHERE DeleteFg = 0");

The index checks if the name is unique when the deleted flag is false.

This still didn't allow me to create a User with the same name as a deleted user. From looking at the source code Identity Source there is a private method that checks if the name is unique. Is there a way to disable this validation?

Or will I need to overwrite some of the Identity methods to check for a user where the delete flag is false.

Has anyone done this before, is it a lot of work or is better way to do this?

I'm afraid if I start doing this I'm going to end up down a big rabbit hole


Solution

  • You are correct about the rabbit hole - if you start digging it, you'll end up with a lot of code just for that purpose. Easy way to do logical deletes is to append some suffix to end of a username, so the duplication does not happen.

    I'm thinking suffix should contain some random value as well, so you can delete the same user multiple times.