本文整理汇总了C#中UserManager.AddToRoles方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.AddToRoles方法的具体用法?C# UserManager.AddToRoles怎么用?C# UserManager.AddToRoles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.AddToRoles方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: seedUserRoles
private static void seedUserRoles(string Admin, string User, ApplicationDbContext db)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
manager.AddToRoles(Admin, Roles.ADMIN);
manager.AddToRole(User, Roles.USER);
}
示例2: Seed
public void Seed(ApplicationDbContext context)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (!context.Users.Any(u => u.UserName == "IvkoBivko"))
{
var user = new ApplicationUser
{
Email = "[email protected]",
UserName = "IvkoBivko",
CreatedOn = DateTime.Now
};
userManager.Create(user, "123456");
userManager.AddToRoles(
user.Id,
ApplicationRoles.Admin,
ApplicationRoles.Artist,
ApplicationRoles.Designer,
ApplicationRoles.Regular,
ApplicationRoles.Seller,
ApplicationRoles.Student,
ApplicationRoles.Trainer);
}
}
示例3: AddUserToRole
/// <summary>
/// Adds user to a AspNetRole and all other roles that are lower in the Roles Enum hierarchy
/// </summary>
/// <param name="user">Application User</param>
/// <param name="role">Roles enum role</param>
public static void AddUserToRole(ApplicationUser user, Roles role)
{
var context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
List<string> roles = new List<string>();
if (roleManager.RoleExists(role.ToString()))
{
//ascertain level, add to this role and every role beneath it
int level = (int)role;
foreach (Roles roleVal in Enum.GetValues(typeof(Roles)))
{
if ((int)roleVal >= level)
{
roles.Add(roleVal.ToString());
}
}
UserManager.AddToRoles(user.Id, roles.ToArray());
}
else
{
throw new Exception("Role does not exist");
}
}
示例4: Create
public ActionResult Create([Bind(Include = "FirstName,LastName,Email,PhoneNumber,UserName")] ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.FindByName(applicationUser.UserName);
if (user == null)
{
userManager.Create(applicationUser);
userManager.SetLockoutEnabled(applicationUser.Id, false);
userManager.AddToRoles(applicationUser.Id, Request["Role"]);
return RedirectToAction("Index");
}
}
return View(applicationUser);
}
示例5: Edit
public ActionResult Edit([Bind(Include = "FirstName,LastName,Email,PhoneNumber,UserName")] ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
string userId = userManager.FindByName(applicationUser.UserName).Id;
userManager.RemoveFromRoles(userId, userManager.GetRoles(userId).ToArray());
userManager.AddToRoles(userId, Request["Role"].Split(','));
userManager.Update(applicationUser);
return RedirectToAction("Index");
}
return View(applicationUser);
}
示例6: seedUserRoles
private static void seedUserRoles(string User, string Tech, string Admin, string Root, string User2, string User3, ApplicationDbContext db)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
manager.AddToRole(User, Roles.CUSTOMER);
manager.AddToRoles(Tech, Roles.TECH, Roles.CUSTOMER);
manager.AddToRoles(Admin, Roles.ADMIN, Roles.TECH, Roles.CUSTOMER);
manager.AddToRoles(Root, Roles.ROOT, Roles.ADMIN, Roles.TECH, Roles.CUSTOMER);
manager.AddToRole(User2, Roles.CUSTOMER);
manager.AddToRole(User3, Roles.CUSTOMER);
}
示例7: CreateUser
public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var createUser = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = new ApplicationUser()
{
UserName = createUserModel.Username,
Email = createUserModel.Email,
FirstName = createUserModel.FirstName,
LastName = createUserModel.LastName,
Level = 3,
JoinDate = DateTime.Now.Date,
};
IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);
if (!addUserResult.Succeeded)
{
return GetErrorResult(addUserResult);
}
string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
await this.AppUserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
var adminUser = createUser.FindByName(user.UserName);
createUser.AddToRoles(adminUser.Id, new string[] { "User" });
return Created(locationHeader, TheModelFactory.Create(user));
}