本文整理汇总了C#中ApplicationUserManager.AddUserToRolesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.AddUserToRolesAsync方法的具体用法?C# ApplicationUserManager.AddUserToRolesAsync怎么用?C# ApplicationUserManager.AddUserToRolesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationUserManager
的用法示例。
在下文中一共展示了ApplicationUserManager.AddUserToRolesAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
if (args == null || args.Length < 3)
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
string exeName = Path.GetFileName(codeBase);
Console.WriteLine("usage: {0} <username> <password> <role>", exeName);
return;
}
var username = args[0];
var password = args[1];
var role = args[2];
var mongoContext = new MongoContext();
var identityContext = new ApplicationIdentityContext(mongoContext);
var store = new UserStore<ApplicationUser>(identityContext);
var manager = new ApplicationUserManager(store);
var user = new ApplicationUser { UserName = username };
var result = manager.Create(user, password);
if (result.Succeeded)
{
var roles = new[] { role };
var roleResult = manager.AddUserToRolesAsync(user.Id, roles);
roleResult.Wait();
Console.WriteLine("user created!");
Console.ReadKey();
return;
}
foreach (var error in result.Errors)
{
Console.WriteLine(error);
}
Console.ReadKey();
}
示例2: 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);
}