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


C# ApplicationUserManager.IsInRole方法代码示例

本文整理汇总了C#中ApplicationUserManager.IsInRole方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.IsInRole方法的具体用法?C# ApplicationUserManager.IsInRole怎么用?C# ApplicationUserManager.IsInRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ApplicationUserManager的用法示例。


在下文中一共展示了ApplicationUserManager.IsInRole方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeAppEnvironment

        private void InitializeAppEnvironment()
        {
            //app environment configuration
            using (var db = new ApplicationDbContext())
            {
                db.Database.CreateIfNotExists();
                var roleStore = new RoleStore<IdentityRole>(db);
                var role = roleStore.FindByNameAsync("Admin").Result;
                if (role == null)
                {
                    roleStore.CreateAsync(new IdentityRole("Admin")).Wait();
                }

                var userStore = new UserStore<ApplicationUser>(db);
                var manager = new ApplicationUserManager(userStore);

                var admin = manager.FindByName("admin");
                if (admin == null)
                {
                    admin = new ApplicationUser
                    {
                        UserName = "admin",
                        Email = "[email protected]",
                        EmailConfirmed = true,
                        CreateDate = DateTime.Now
                    };
                    var r = manager.CreateAsync(admin, "~Pwd123456").Result;
                }
                if (!manager.IsInRole(admin.Id, role.Name))
                {
                    manager.AddToRole(admin.Id, role.Name);
                }
            }
        }
开发者ID:kissstudio,项目名称:Topawes,代码行数:34,代码来源:Startup.cs

示例2: ChangeRole

        public ActionResult ChangeRole( string id )
        {
            Person p = db.Users.Where( pp => id == pp.Id ).FirstOrDefault();

            var userManager = new ApplicationUserManager( new UserStore<Person>( db ) );

            if( userManager.IsInRole( p.Id, "editor" ) ) {
                userManager.RemoveFromRole( p.Id, "editor" );
                userManager.AddToRole( p.Id, "admin" );
            } else if( userManager.IsInRole( p.Id, "admin" ) ) {
                userManager.RemoveFromRole( p.Id, "admin" );
                userManager.AddToRole( p.Id, "user" );
            } else {
                userManager.RemoveFromRole( p.Id, "user" );
                userManager.AddToRole( p.Id, "editor" );
            }

            db.SaveChanges();

            return RedirectToAction( "AdminTable" );
        }
开发者ID:ylyubimov,项目名称:Portal,代码行数:21,代码来源:AdminController.cs

示例3: 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

示例4: DeleteRoleForUser

        public ActionResult DeleteRoleForUser(string UserName, string RoleName)
        {
            try
            {
                var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));

                ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

                if (manager.IsInRole(user.Id, RoleName))
                {
                    manager.RemoveFromRole(user.Id, RoleName);
                    TempData["ValidationMessage"] = ("Success: " + " " + UserName + " " + "Was Removed From the " + " " + RoleName + " " + "Role");
                }
                else
                {
                    TempData["ValidationMessage"] = ("Error: " + " " + UserName + " " + "Was Not Successfully Removed From the " + " " + RoleName + " " + "Role");
                }
            }
            catch
            {
                TempData["ValidationMessage"] = ("Error: Something Went Wrong");
            }
            // prepopulat roles for the view dropdown
            var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            ViewBag.Roles = list;

            return View("ManageRole");
        }
开发者ID:amoryjh,项目名称:MVC-HPFS,代码行数:28,代码来源:UserController.cs

示例5: Edit

        public ActionResult Edit([Bind(Include = "UserId,Roles,UserName,Email")] UserEdit userEdit)
        {
            if (userEdit.UserId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var user = _db.Users.Find(userEdit.UserId);
            if (user == null)
            {
                return HttpNotFound();
            }
            if (!ModelState.IsValid) return View(userEdit);

            _userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            foreach (var role in userEdit.Roles)
            {
                if (role.IsChecked && !_userManager.IsInRole(user.Id, role.Name))
                    _userManager.AddToRole(user.Id, role.Name);
                if (!role.IsChecked && _userManager.IsInRole(user.Id, role.Name))
                    _userManager.RemoveFromRole(user.Id, role.Name);
            }
            _db.SaveChanges();
            // This was intended to force roles to update without manual logout login, but for now it's more trouble than it's worth
            /*var authenticationManager = HttpContext.GetOwinContext().Authentication;
            authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
            authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);*/
            return RedirectToAction("Index");
        }
开发者ID:Methodician,项目名称:ScatterSchool,代码行数:29,代码来源:UsersController.cs


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