Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 241
jenifer Avatar asked Mar 25 '11 23:03

jenifer


1 Answers

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);            
}
like image 130
smartcaveman Avatar answered Nov 12 '22 22:11

smartcaveman