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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.