Membership.DeleteUser is not deleting all related rows of the user
Asked Answered
L

1

3

Membership.DeleteUser() manages to delete the userprofile of the user I am deleting. Why does it not also delete it's information stored in webpages_membership.

I am not using roles at this point in time so keep that in mind for your answers.

Laius answered 5/3, 2013 at 20:0 Comment(0)
V
9

I ran a test and verified that SimpleMembershipProvider.DeleteUser does not work as advertised. According to the documentation:

This method deletes the entry in the membership account table (by default, webpages_Membership). If deleteAllRelatedData is true, all user data that is stored in the user table is also deleted.

But in my test I set deleteAllRelatedData to true and it just deleted the entry in the UserProfile table, leaving the entry in webpages_membership untouched. Either a bug in the documentation or in the implementation of SimpleMembership.

But I did find a work around. First call SimpleMembershipProvider.DeleteAccount. This will delete the entry in the webpages_membership table. Then call SimpleMembershipProvider.DeleteUser to remove the entry in the UserProfile table. Here is a snippet of the code I used for my test.

    var roles = (SimpleRoleProvider)Roles.Provider;
    var membership = (SimpleMembershipProvider)Membership.Provider;

    if (!roles.RoleExists("Admin"))
    {
        roles.CreateRole("Admin");
    }
    if (membership.GetUser("test", false) == null)
    {
        membership.CreateUserAndAccount("test", "test");
    }

    //Commented this out because you will get a foreign key 
    //error if you try to delete the user without removing the 
    //the mapping of the user to a role
    //if (!roles.GetRolesForUser("test").Contains("Admin"))
    //{
    //    roles.AddUsersToRoles(new[] { "test" }, new[] { "admin" });
    //}

    //This will delete the user information from webpages_membership
    bool wasDeleted = membership.DeleteAccount("test");

    //This will delelet the user information form UserProfile
    wasDeleted = membership.DeleteUser("test", true);

As you can see from the comments this will not work if you are using roles and you have roles mapped to that user. You will need to remove those before you can remove the user. I know this is not an issue for zms6445, but I wanted to put this out there for people using roles.

Vaporing answered 5/3, 2013 at 21:21 Comment(3)
How do you access the method DeleteAccount and DeleteUser. I attempted your workaround and the only methods available to me in the SimpleMembershipProvider class is Equals and ReferenceEquals.Laius
I added the code for my test in the Answer. Hopefully this will answer your question. If it does not provide code samples in your Question so I can assist.Vaporing
Thanks Kevin, great answer. I added to your answer code for deleting a user from roles. foreach (var role in Roles.GetRolesForUser("test")) Roles.RemoveUserFromRole("test", role); #5439569Jalap

© 2022 - 2024 — McMap. All rights reserved.