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


C# ApplicationUserManager.RemoveFromRole方法代码示例

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


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

示例1: ClearUserRoles

        public void ClearUserRoles(ApplicationUserManager userManager, string userId)
        {
            var user = userManager.FindById(userId);
            var currentRoles = new List<IdentityUserRole>();

            currentRoles.AddRange(user.UserRoles);
            foreach (var role in currentRoles)
            {
                userManager.RemoveFromRole(userId, role.RoleId);
            }
        }
开发者ID:GosuKapu,项目名称:BzzCMS,代码行数:11,代码来源:RolesHelper.cs

示例2: ClearUserRoles

 public void ClearUserRoles(string userId)
 {
     var um = new ApplicationUserManager(
         new UserStore<ApplicationUser>(context));
     var user = um.FindById(userId);
     var currentRoles = new List<IdentityUserRole>();
     currentRoles.AddRange(user.Roles);
     foreach (var role in currentRoles)
     {
         um.RemoveFromRole(userId, role.RoleId);
     }
 }
开发者ID:BenVandenberk,项目名称:EindwerkNET,代码行数:12,代码来源:IdentityModels.cs

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

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

示例5: InsertOrUpdate

        public async Task<bool> InsertOrUpdate(IdentityUserViewModel model, ApplicationUserManager userManager)
        {

            var user = await userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                user = new ApplicationUser();
                user.Assign(model);


                var result = await userManager.CreateAsync(user, "1234567");
                if (!result.Succeeded) return false;
                model.Id = user.Id;
            }
            else
            {
                user.Email = model.Email;
                user.UserName = model.UserName;
                user.DUI = model.DUI;
                user.PhoneNumber = model.PHONE_2;
                user.ADDRESS = model.ADDRESS;
                user.Category = model.Category;
                user.FirstName = model.FirstName;
                user.LastName = model.LastName;
                user.DUI = model.DUI;
                user.PHONE_2 = model.PHONE_2;
                user.ProfilePicture = model.ProfilePicture;
                user.CenterId = model.CenterId;
                //user.Address = model.Address;
                //user.FirstName = model.FirstName;
                //user.LastName = model.LastName;
                //user.DocumentNum = model.DocumentNum;
                //user.ProfilePicture = model.ProfilePicture;
                await userManager.UpdateAsync(user);
            }

            if (model.ForceChangePassword)
            {
                var tok = await userManager.GeneratePasswordResetTokenAsync(model.Id);

                var result = await userManager.ResetPasswordAsync(model.Id, tok, model.Password);

                if (!result.Succeeded) return false;
            }
            var roles = await userManager.GetRolesAsync(model.Id);

            if (!roles.Any() && !string.IsNullOrEmpty(model.Role))
            {
                var res = await userManager.AddToRoleAsync(model.Id, model.Role);
            }

            if (roles.All(r => r != model.Role) && roles.Any())
            {
                var result = await userManager.AddToRoleAsync(model.Id, model.Role);
            }
            roles.Where(r => r != model.Role).ToList().ForEach(role => userManager.RemoveFromRole(model.Id, role));

            if (model.CenterId != 0)
            {

                var claim = await Context.UserClaims.FirstOrDefaultAsync(c => c.UserId == user.Id && c.ClaimType == "CenterId");

                var claims = await userManager.GetClaimsAsync(user.Id);
                if (claim != null)
                {
                    claim.ClaimValue = model.CenterId.Value.ToString();
                    await Context.SaveChangesAsync();
                }
                else
                {
                    await userManager.AddClaimAsync(model.Id, new System.Security.Claims.Claim("CenterId", model.CenterId.ToString()));
                }
            }


            return true;
        }
开发者ID:QualityConsultingTeam,项目名称:Cancha,代码行数:78,代码来源:IdentityManagerService.cs

示例6: UserEdit

        public ActionResult UserEdit(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                //userManager = new ApplicationUserManager(new ApplicationUserStore(identityDb));
                userManager = new ApplicationUserManager(new ApplicationUserStore(identityDb));

                var currentUser = identityDb.Users.Find(model.UserId);
                var currentRoles = currentUser.Roles;
                var currentRoleIds = currentRoles.Select(r => r.RoleId);

                var selectedRoles = new HashSet<int>();
                if (model.SelectedRoles != null)
                    selectedRoles = new HashSet<int>(model.SelectedRoles);

                foreach (var role in identityDb.Roles.ToList())
                {
                    if (currentRoleIds.Contains(role.Id) && !selectedRoles.Contains(role.Id))
                    {
                        userManager.RemoveFromRole(currentUser.Id, role.Name);
                    }
                    else if (!currentRoleIds.Contains(role.Id) && selectedRoles.Contains(role.Id))
                    {
                        userManager.AddToRole(currentUser.Id, role.Name);
                    }
                }
                return RedirectToAction("Users");
            }
            return View(model);
        }
开发者ID:jienfei,项目名称:nspoc,代码行数:30,代码来源:ManagementController.cs

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

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

示例9: RemoveFromRole

 public void RemoveFromRole(ApplicationUserManager userManager, string userId, string roleName)
 {
     userManager.RemoveFromRole(userId, roleName);
 }
开发者ID:GosuKapu,项目名称:BzzCMS,代码行数:4,代码来源:RolesHelper.cs


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