0

I have successfully created custom users and roles within the Asp.Net Identity framework, however one section which is lacking is support for the 'Roles' collection within a User. I added a logical 'Deleted' flag within a UserRole and I want this to be included when EF retrieves the info from the database, however all it does is retrieve everything (which isn't what I want). Senario:

  1. User A has a Role of 'admin' (non-deleted)
  2. I then add a Role of 'super-admin' and then delete 'admin'
  3. If I then access my version of IdentityUser (which has the 'Roles' ICollection - custom type) I still see the 2 roles (1 deleted, 1 not)

What I want to happen is on login (using mostly default functions with various sections overridden), when EF retrieves the data it should know to exclude any row which is flagged as deleted, however I have been unable to find any method of doing this. Because all the boilerplate code is locked, I cannot easily see what is happening or find any logical place to override this functionality.

Can anyone help with this?

1 Answers1

0

You could just scan the roles after login and remove them with code similar to this:

        var roles = UserManager.GetRoles(userId);
        foreach (var roleName in roles)
        {
            var role = RoleManager.FindByName(roleName);
            if (role.IsDeleted) UserManager.RemoveFromRole(userId, roleName);
        }
Steve Greene
  • 12,029
  • 1
  • 33
  • 54