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


C# UserManager.AddToRoles方法代码示例

本文整理汇总了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);
        }
开发者ID:tiemahshoo,项目名称:Sample,代码行数:7,代码来源:Seeder.cs

示例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);
            }
        }
开发者ID:IvoPaunov,项目名称:Course-Project-ASP.NET-MVC,代码行数:26,代码来源:AdminsSeeder.cs

示例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");
            }
        }
开发者ID:Eppern,项目名称:CocktailAlmanac,代码行数:31,代码来源:UserHelpers.cs

示例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);
        }
开发者ID:ChrisSallee,项目名称:Kanban,代码行数:20,代码来源:ApplicationUsersController.cs

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

示例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);
 }
开发者ID:giorosati,项目名称:envision4it,代码行数:10,代码来源:Seeder.cs

示例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));
        }
开发者ID:riyadh21,项目名称:AspIdentity.WebApi,代码行数:39,代码来源:AccountsController.cs


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