本文整理汇总了C#中ApplicationRoleManager.RoleExists方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationRoleManager.RoleExists方法的具体用法?C# ApplicationRoleManager.RoleExists怎么用?C# ApplicationRoleManager.RoleExists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationRoleManager
的用法示例。
在下文中一共展示了ApplicationRoleManager.RoleExists方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 "无效的操作指令";
}
}
示例2: Post
public void Post(RoleUsersDto dto)
{
var context = ApplicationDbContext.Create();
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
if (!roleManager.RoleExists(dto.RoleName)) return;
foreach (
var user in
dto.UserNames.Select(userName => userManager.FindByName(userName))
.Where(user => user != null)
.Where(user => !userManager.IsInRole(user.Id, dto.RoleName)))
{
userManager.AddToRole(user.Id, dto.RoleName);
}
foreach (
var user in
dto.UserNames.Select(userName => userManager.FindByName(userName))
.Where(user => user != null)
.Where(user => userManager.IsInRole(user.Id, dto.RoleName)))
{
userManager.RemoveFromRole(user.Id, dto.RoleName);
}
}
示例3: RoleExists
public bool RoleExists(string name)
{
var rm = new ApplicationRoleManager(
new RoleStore<IdentityRole>(context));
return rm.RoleExists(name);
}
示例4: 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);
}
示例5: 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();
}