本文整理汇总了C#中UserManager.AddToRolesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.AddToRolesAsync方法的具体用法?C# UserManager.AddToRolesAsync怎么用?C# UserManager.AddToRolesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.AddToRolesAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeUserAdmin
public IHttpActionResult MakeUserAdmin(string username)
{
var user = this.Data.Users.All().ToList().FirstOrDefault(u => u.UserName == username);
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
string adminCheck = um.GetRoles(user.Id).FirstOrDefault(a => a.Contains("AppAdmin"));
if (adminCheck != string.Empty)
{
return this.BadRequest(string.Format("user {0} is allready admin", user.UserName));
}
um.AddToRolesAsync(user.Id, "AppAdmin");
this.Data.SaveChanges();
return this.Ok(string.Format("user: {0} is admin", user.UserName));
}
示例2: CreateUserSync
private static async Task CreateUserSync(UserManager<ApplicationUser> userManager, UserTemplate userTemplate, ApplicationUser user)
{
await userManager.CreateAsync(user, userTemplate.Password);
await userManager.AddToRolesAsync(user, userTemplate.Roles);
}
示例3: AddUsersToRoles
private static void AddUsersToRoles(UserManager<ApplicationUser> users, RoleManager<ApplicationRole> roleManager)
{
var admin = users.FindByEmailAsync("[email protected]").Result;
var esthetician = users.FindByEmailAsync("[email protected]").Result;
Task.FromResult(users.AddToRolesAsync(admin, new[] { "Admin", "Esthetician" }).Result);
Task.FromResult(users.AddToRoleAsync(esthetician, "Esthetician").Result);
}