当前位置: 首页>>代码示例>>C#>>正文


C# ApplicationRoleManager.RoleExists方法代码示例

本文整理汇总了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 "无效的操作指令";
            }

        }
开发者ID:ouyh18,项目名称:LtePlatform,代码行数:20,代码来源:ApplicationRolesController.cs

示例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);
            }
        }
开发者ID:ouyh18,项目名称:LtePlatform,代码行数:24,代码来源:ApplicationRolesController.cs

示例3: RoleExists

 public bool RoleExists(string name)
 {
     var rm = new ApplicationRoleManager(
         new RoleStore<IdentityRole>(context));
     return rm.RoleExists(name);
 }
开发者ID:BenVandenberk,项目名称:EindwerkNET,代码行数:6,代码来源:IdentityModels.cs

示例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);
        }
开发者ID:Pyuuma,项目名称:SITE,代码行数:36,代码来源:AccountController.cs

示例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();
        }
开发者ID:Self-Education-Association,项目名称:SITE,代码行数:54,代码来源:AdministratorController.cs


注:本文中的ApplicationRoleManager.RoleExists方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。