本文整理汇总了C#中ApplicationRoleManager.CreateAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationRoleManager.CreateAsync方法的具体用法?C# ApplicationRoleManager.CreateAsync怎么用?C# ApplicationRoleManager.CreateAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationRoleManager
的用法示例。
在下文中一共展示了ApplicationRoleManager.CreateAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Application_Start
protected async void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
// ユーザーとロールの初期化
// ロールの作成
var roleManager = new ApplicationRoleManager(new UserStore());
await roleManager.CreateAsync(new ApplicationRole { Name = "admin" });
await roleManager.CreateAsync(new ApplicationRole { Name = "users" });
var userManager = new ApplicationUserManager(new UserStore());
// 一般ユーザーの作成
await userManager.CreateAsync(new ApplicationUser { UserName = "tanaka" }, "[email protected]");
await userManager.AddToRoleAsync(
(await userManager.FindByNameAsync("tanaka")).Id,
"users");
// 管理者の作成
await userManager.CreateAsync(new ApplicationUser { UserName = "super_tanaka" }, "[email protected]");
await userManager.AddToRoleAsync(
(await userManager.FindByNameAsync("super_tanaka")).Id,
"users");
await userManager.AddToRoleAsync(
(await userManager.FindByNameAsync("super_tanaka")).Id,
"admin");
Debug.WriteLine("-----------");
}
示例2: Get
public async Task<string> Get(string roleName, string action)
{
var context = ApplicationDbContext.Create();
var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
switch (action)
{
case createAction:
if (roleManager.RoleExists(roleName)) return "新增角色失败,该角色名称已存在";
await roleManager.CreateAsync(new ApplicationRole(roleName));
return "新增角色成功";
case deleteAction:
if (!roleManager.RoleExists(roleName)) return "删除角色失败,该角色名称不存在";
var role = roleManager.FindByName(roleName);
await roleManager.DeleteAsync(role);
return "删除角色成功";
default:
return "无效的操作指令";
}
}
示例3: Register
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User { UserName = model.Email, Email = model.Email, DisplayName = model.DisplayName, Time = DateTime.Now, IsDisabled = false, Profile = new Profile { Email = model.Email, Phone = "", Searchable = true, InformationPrivacy = false, Other = "" } };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
//为账户添加角色
var roleName = "student";
ApplicationRoleManager roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(new BaseDbContext()));
//判断角色是否存在
if (!roleManager.RoleExists(roleName))
{
//角色不存在则建立角色
await roleManager.CreateAsync(new IdentityRole(roleName));
}
//将用户加入角色
await UserManager.AddToRoleAsync(user.Id, roleName);
// 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
// 发送包含此链接的电子邮件
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
示例4: TutorCreate
public async System.Threading.Tasks.Task<ActionResult> TutorCreate(CreateTutorViewModel model)
{
if (ModelState.IsValid)
{
if (Request.Files.Count != 1)//如果文件列表为空则返回
{
ViewBag.Alert = "请检查上传文件!";
return View();
}
var file = Request.Files[0];//只上传第一个文件
var user = Models.User.Create(model.Email, model.DisplayName);
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
//为账户添加角色
var roleName = "Tutor";
ApplicationRoleManager roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(db));
//判断角色是否存在
if (!roleManager.RoleExists(roleName))
{
//角色不存在则建立角色
await roleManager.CreateAsync(new IdentityRole(roleName));
}
//将用户加入角色
await UserManager.AddToRoleAsync(user.Id, roleName);
// 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
// 发送包含此链接的电子邮件
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");
var avatar = Material.Create(user.DisplayName, MaterialType.Avatar, file, db);
if (avatar == null)
{
TempData["ALert"] = "请检查上传文件!";
return View(model);
}
var tutor = new TutorInformation { Id = Guid.NewGuid(), Tutor = db.Users.Find(user.Id), Avatar = avatar, Position = model.Position, Introduction = model.Introduction };
db.TutorInformations.Add(tutor);
db.SaveChanges();
ViewBag.Alert = "操作成功!";
return View();
}
}
ViewBag.Alert = "操作失败!";
return View();
}
示例5: SaveChanges
public async Task<IdentityResult> SaveChanges(JObject newRole)
{
string roleName = ((dynamic)newRole).Name.Value;
string roleId = ((dynamic)newRole).Id.Value;
//dynamic oldRole = ((dynamic)newRole).oldRole.Value;
IdentityResult result = null;
var roleStore = new RoleStore<IdentityRole>(_contextProvider.Context);
var roleManager = new ApplicationRoleManager(roleStore);
var role = await roleManager.FindByIdAsync(roleId);
if (role == null)
{
result = await roleManager.CreateAsync(new IdentityRole(roleName));
}
return result;
}