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


C# ApplicationRoleManager.CreateAsync方法代码示例

本文整理汇总了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("-----------");
        }
开发者ID:runceel,项目名称:ASPNETIdentity,代码行数:28,代码来源:Global.asax.cs

示例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 "无效的操作指令";
            }

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

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

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

示例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;
        }
开发者ID:szwork2013,项目名称:BoiPlt,代码行数:18,代码来源:RoleManagerController.cs


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