在添加之前,用户存在检查角色

时间:2017-03-10 12:57:40

标签: c# asp.net-mvc asp.net-identity roles

我正在尝试为用户添加角色但在此之前我想检查它是否存在。我怎样才能做到这一点? 这是我的代码

  public void AddRoleForUser(ApplicationUser obj, IdentityRole role)
    {
        _userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(_context));

        var currentUser = _userManager.FindById(obj.Id);
        // before this i have to check 
        var roleresult = _userManager.AddToRole(currentUser.Id, role.Name);
    }

例如我有一个用户,其id = 1。当我为该用户添加角色时,我想在向该用户添加新角色之前检查该用户是否有角色

1 个答案:

答案 0 :(得分:5)

您只需查看private boolean stopTimer(List<CountDownTimer> listOfTimers, int index) { if(listOfTimers != null && listOfTimers.size() > index) { for(int i = 0; i < listOfTimers.size(); i++) { listOfTimers.get(i).cancel(); } return true; } return false; }

即可

如果您想通过 User.IsInRole("YourRoleName"); 进行检查,请使用以下代码。

User Id