本文整理汇总了C#中ApplicationUserManager.RemoveUserFromRolesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.RemoveUserFromRolesAsync方法的具体用法?C# ApplicationUserManager.RemoveUserFromRolesAsync怎么用?C# ApplicationUserManager.RemoveUserFromRolesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationUserManager
的用法示例。
在下文中一共展示了ApplicationUserManager.RemoveUserFromRolesAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateRoles
public async Task<IdentityResult> UpdateRoles(JObject roles)
{
string userId = ((dynamic)roles).Id.Value;
string roleId = ((dynamic)roles).RoleId.Value;
string roleName = ((dynamic)roles).RoleName.Value;
IdentityResult result = null;
#region TODO: Remove unused commented code
//IdentityRole role = (from r in _contextProvider.Context.Roles where r.Id == roleId select r).FirstOrDefault();
//var user = _contextProvider.Context.Users.Where(x => x.Id == userId).FirstOrDefault();
//if (user.Roles.Any(x => x.RoleId == roleId))
//{
// // User is in the Admin Role
//}
//else
//{
// //User is not in the Admin Role
//}
//var role = (from r in _contextProvider.Context.Roles where r.Id == roleId select r).FirstOrDefault();
//var users = _contextProvider.Context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList();
//if (users.Find(x => x.Id == userId) != null)
//{
// // User is in the Admin Role
//}
//else
//{
// //User is not in the Admin Role
//}
#endregion
var userStore = new UserStore<ApplicationUser>(_contextProvider.Context);
var userManager = new ApplicationUserManager(userStore);
var roleStore = new RoleStore<IdentityRole>(_contextProvider.Context);
var roleManager = new ApplicationRoleManager(roleStore);
ApplicationUser user = await userManager.FindByIdAsync(userId);//.ConfigureAwait(false);
IList<string> role = new List<string>();
role.Add(roleName);
if (user.Roles.Any(x => x.RoleId == roleId))
{
//remove user and roll mapping
result = await userManager.RemoveUserFromRolesAsync(userId, role).ConfigureAwait(false);
result = await userManager.RemoveClaimAsync(userId, new Claim(IdentityConstants.ClaimTypes.Role, roleName));
}
else
{
result = await userManager.AddUserToRolesAsync(userId, role).ConfigureAwait(false);
result = await userManager.AddClaimAsync(userId, new Claim(IdentityConstants.ClaimTypes.Role, roleName));
//User is not in the Admin Role
}
#region TODO: Remove unused commented code
//if (user.Roles.Count > 0)
//{
// var roleExists = user.Roles.First(x => x.RoleId == roleId);
// if (roleExists != null)
// {
// //remove user and roll mapping
// //result = await userManager.RemoveFromRoleAsync(userId, roleId).ConfigureAwait(false);
// //remove user and its claim
// //Claim c = new Claim();
// //c.Type = "role";
// //c.Value
// //result = await userManager.RemoveClaimAsync(userId,new Claim() { })
// }
// else
// {
// //result = await userManager.AddToRoleAsync(userId,roleId);
// }
//}
#endregion
return await userManager.UpdateAsync(user).ConfigureAwait(false);
}