Membership.DeleteUser(UserName,true) 不从角色中删除用户
当我单击“删除”链接按钮时,它可以从“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UserInRole
表包含两个Guid
字段。不存储用户名。每当创建新用户时,都会为他们分配一个新的、完全唯一的UserId
。您认为为什么用户角色关联没有被删除?您可以通过执行
SELECT COUNT(*) FROM aspnet_UserInRoles WHERE UserId={DeletedUserId}
查询来准确测试这一点。我在下面提供了 ASP.NET 成员资格 SQL 数据库架构供您参考。
此外,
Roles
API 允许您手动删除角色。因此,删除给定用户的所有角色将如下所示:The
UserInRole
table contains twoGuid
fields. The username is not stored. Whenever a new User is created, they are assigned a new, completely uniqueUserId
.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.
Also, the
Roles
API allows you to delete roles manually. So, to delete all the roles for a given user would looke like: