Membership.DeleteUser(UserName,true) not removing user from role

When I click the “delete” linkbutton, it can delete the all User info from my “UserDetail” table in my “JobPost.mdf”, it also delete the corresponding “aspnet_Users” & “aspnet_Membership”,but the “UserInRole” still contain that UserName. Even though I specified the Code:Membership.DeleteUser(UserName, true);

I thought true is for bool deleteallrelated data, but it doesn’t really delete the userInRole. So next time the user registers with the same name, it automatically get the “admin” role right.

This “deleteUser” page I keep it inside a protected “admin”folder.

How to solve it? Why Membership.DeleteUser(UserName, true) doesn’t delete UserInRole?

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
     if (e.CommandName == "Delete")
     {
         string UserName = e.CommandArgument.ToString();
         Membership.DeleteUser(UserName, true);
         JobPostDataContext db = new JobPostDataContext();
         var query = from u in db.UserDetails
                     where u.UserName == UserName
                     select u;
         foreach (var item in query)
         {
             db.UserDetails.DeleteOnSubmit(item);
         }
         db.SubmitChanges();

         FormsAuthentication.SignOut();    
     }
 }

My web.config inside the protected Admin folder:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.web>
       <authorization>
            <allow roles="Administrators" />
            <deny users="*" />
        </authorization>
    </system.web>
</configuration>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The UserInRole table contains two Guid fields. The username is not stored. Whenever a new User is created, they are assigned a new, completely unique UserId.

Why do you think the user-role association is not being deleted? You can test this accurately by performing a query for the SELECT COUNT(*) FROM aspnet_UserInRoles WHERE UserId={DeletedUserId}.

I have included the ASP.NET membership SQL database schema below for your reference.
enter image description here

Also, the Roles API allows you to delete roles manually. So, to delete all the roles for a given user would looke like:

void DeleteUserRoles(string username)
{
    foreach (var role in Roles.GetRolesForUser(username))
        Roles.RemoveUserFromRole(username, role);            
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x