Membership.DeleteUser(UserName,true) 不从角色中删除用户

发布于 2024-10-26 21:51:26 字数 1401 浏览 2 评论 0原文

当我单击“删除”链接按钮时,它可以从“JobPost.mdf”中的“UserDetail”表中删除所有用户信息,它还会删除相应的“aspnet_Users”和“aspnet_Users”。 “aspnet_Membership”,但“UserInRole”仍然包含该用户名。即使我指定了代码:Membership.DeleteUser(UserName, true);

我认为true是针对bool deleteall相关数据,但它并没有真正删除userInRole。因此,下次用户使用相同的名称注册时,它会自动获得“admin”角色。

我将这个“deleteUser”页面保存在受保护的“admin”文件夹中。

怎么解决呢?为什么 Membership.DeleteUser(UserName, true) 不删除 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();    
     }
 }

我的 web.config 在受保护的 Admin 文件夹中:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-11-02 21:51:26

UserInRole 表包含两个 Guid 字段。不存储用户名。每当创建新用户时,都会为他们分配一个新的、完全唯一的UserId

您认为为什么用户角色关联没有被删除?您可以通过执行 SELECT COUNT(*) FROM aspnet_UserInRoles WHERE UserId={DeletedUserId} 查询来准确测试这一点。

我在下面提供了 ASP.NET 成员资格 SQL 数据库架构供您参考。
在此处输入图像描述

此外,Roles API 允许您手动删除角色。因此,删除给定用户的所有角色将如下所示:

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

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);            
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文