0

我的应用程序中有执行此操作的现有代码:

if (HttpContext.Current.User.IsInRole("Customer Account Admin"))
                    //
                    {
                    }
                    else
                    {
                        mi = radmenu1.Items.FindItemByText("Admin");
                        radmenu1.Items.Remove(mi);
                    }

是什么让用户担任该角色 我们没有使用旧的角色管理器。我们正在使用 ASP.NET Identity 2.0,但这似乎没有这样做:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();


    if (manager.AddToRole(manager.FindByName(UserName.Text).Id, "Customer Account Admin").Succeeded)
    {
       c.logActivity("Register.aspx.cs", "REG_USER_ROLE", "Setting user to Admin role succeeded");
                                    }
    }
4

1 回答 1

1

您可以使用IsInRole

private ApplicationUserManager _userManager;

public ApplicationUserManager UserManager 
{
   get 
   {
      if (_userManager == null) 
      {
         _userManager = HttpContext.GetOwinContext()
             .GetUserManager<ApplicationUserManager>();
      }
      return _userManager;
   }
}

bool isAdmin = UserManager.IsInRole(User.Identity.GetUserId(), 
   "Customer Account Admin");
于 2014-04-18T19:37:18.283 回答