【问题标题】:users in one role cannot remove users from another (C#)一个角色的用户不能从另一个角色中删除用户(C#)
【发布时间】:2012-08-09 13:39:56
【问题描述】:

我正在开发一个网站并拥有 3 个基本用户角色。角色是“admin”、“manager”和“user”。我已经阻止了某些页面的基本用户,但允许管理员和经理访问其他人。我创建了一个单独的页面来删除用户,但是,我不想让“经理”角色的人能够从“管理员”角色中删除某人。 “用户”角色无权访问此页面,所以我不担心。我有一个显示所有用户的下拉列表和一个删除所选用户的按钮,它正在工作。我只想添加不允许“经理”角色从“管理员”角色中删除某人的安全性。

这里是我目前的 onClick 事件代码:

string adminuser;
usertodelete = usersddl.SelectedItem.ToString();
if (Roles.GetRolesForUser(usertodelete) = "admin")
   adminuser = "admin";

if (Roles.IsUserInRole("admin") && User.IsInRole("manager"))
    statuslbl.Text = "You do not have sufficient privileges to remove this user.    Only Administrator's can remove administrators from the system.";

else
{
    System.Web.Security.Membership.DeleteUser(usertodelete);
    Response.Redirect("~/Account/DeleteAccount.aspx");
}

我知道此时我的 if 语句在查找和分配某个用户以及检查他们的角色时是错误的。

【问题讨论】:

  • @Surfbutler,这些是框架调用:Roles.IsUserInRole Method
  • 请确保您尝试的代码实际编译。 Roles.GetRolesForUser 返回一个string[],即使在设计时,尝试将其与一个字符串“admin”进行比较时也会引发错误。并且您需要使用== 来比较项目。
  • 为什么不根据角色将自定义源数据绑定到下拉列表,过滤掉具有“管理员”角色的用户,这比不必要的步骤更糟糕。

标签: c# roles


【解决方案1】:

我只想添加不允许“经理”角色从“管理员”角色中删除某人的安全性。

if (Roles.GetRolesForUser(userToDelete).Contains("admin") && !User.IsInRole("admin"))
{   // only allow admins to remove other admins.
   statuslbl.Text = "You do not have sufficient privileges to remove this user.    Only Administrator's can remove administrators from the system."; 
} else {
}

【讨论】:

  • 这正是我想要的。谢谢。我忘记了 .Contains 属性。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2021-04-25
  • 2019-04-27
相关资源
最近更新 更多