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


C# UserManager.RemoveFromRole方法代码示例

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


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

示例1: DeleteRoleForUser

        public ActionResult DeleteRoleForUser(string UserName, string RoleName)
        {
            if (ModelState.IsValid)
            {
                var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

                ViewBag.Token = "2";

                if (userManager.IsInRole(user.Id, RoleName))
                {
                    userManager.RemoveFromRole(user.Id, RoleName);
                    ViewBag.ResultMessage = "Role removed from this user successfully !";
                }
                else
                {
                    ViewBag.ResultMessage = "This user doesn't belong to selected role.";
                }

                // 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;

                var userList = context.Users.OrderBy(r => r.Email).ToList().Select(rr => new SelectListItem { Value = rr.Email.ToString(), Text = rr.Email }).ToList();
                ViewBag.userNames = userList;

                ViewBag.RolesForThisUser = userManager.GetRoles(user.Id);
            }

            return View("ManageUserRoles");
        }
开发者ID:dleivas,项目名称:SpecialBreakFast,代码行数:31,代码来源:RolesController.cs

示例2: DeleteRoleForUser

        public ActionResult DeleteRoleForUser(string UserName, string RoleName)
        {
            var account = new AccountController();
            ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            if (UserManager.IsInRole(user.Id, RoleName))
            {
                UserManager.RemoveFromRole(user.Id, RoleName);
                var employee = context.Employees.Single(x => x.employeeFirstName == user.FirstName && x.employeeLastName ==user.LastName);
                var role = context.RolesForEmployees.Single(x => x.roleName == RoleName);
                var tmp = context.EmployeeRoles.Single(x => x.role == role && x.employee == employee);
                context.EmployeeRoles.Remove(tmp);
                context.SaveChanges();

                ViewBag.ResultMessage = "Role removed from this user successfully !";
            }
            else
            {
                ViewBag.ResultMessage = "This user doesn't belong to selected role.";
            }
            // 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("ManageUserRoles");
        }
开发者ID:Besermenji,项目名称:LeaveManager---WithLogin,代码行数:27,代码来源:RolesController.cs

示例3: Details

        public void Details(AspNetUserDetailsViewModel mdl)
        {

            var ctx = new ApplicationDbContext();
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ctx));

            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ctx));


            //if(userManager.IsInRole(mdl.User.Id, "Admin"))
            //{
            //    mdl.RoleId = RoleManager.FindByName("Admin")?.Id;
            //}
            //if(userManager.IsInRole(mdl.User.Id, "NetUser"))
            //{
            //    mdl.RoleId = RoleManager.FindByName("NetUser")?.Id;
            //}


            var role = RoleManager.FindById(mdl.RoleId);
            var roles = RoleManager.Roles.ToList();
            foreach(var item in roles)
            {
                userManager.RemoveFromRole(mdl.User.Id, item.Name);
            }
            userManager.AddToRole(mdl.User.Id, role.Name);
        }
开发者ID:MULLERDES,项目名称:WCARDUMP,代码行数:27,代码来源:AspNetUsersController.cs

示例4: ListViewUsers_UpdateItem

        public void ListViewUsers_UpdateItem(string Id, string commandArgument)
        {
            var item = this.UsersService.GetById(Id);

            if (item == null)
            {
                var errorMessage = $"User with id {this.ID} was not found";
                Notificator.AddErrorMessage(errorMessage);

                this.ModelState.AddModelError("", errorMessage);
                return;
            }
            
            var isAdmin = ((CheckBox)ListViewAllUsers.Items[ListViewAllUsers.EditIndex].FindControl("CbIsADmin")).Checked;

            // TODO: extract in separate class or service
            var userManager = new UserManager<User>(new UserStore<User>(new XShareDbContext()));

            if (isAdmin)
            {
                userManager.AddToRole(item.Id, "admin");
            }
            else
            {
                userManager.RemoveFromRole(item.Id, "admin");
            }

            this.TryUpdateModel(item);
            if (this.ModelState.IsValid)
            {
                this.UsersService.UpdateUser(item);
                Notificator.AddInfoMessage($"You've just updated the info for user {item.UserName}");
            }
        }
开发者ID:ShareForms,项目名称:XShare,代码行数:34,代码来源:ManageUsers.aspx.cs

示例5: Delete

        // DELETE api/memberships/5
        public void Delete(string id)
        {
            //this is a bit hacky - but the association will be coming in here
            //and it's GUID | role
            //so parse that and unassign
            var splits = id.Split('|');
            var userId = splits[0];
            var roleId = splits[1];

            //this is a new user/role assignment
            using (var db = new ApplicationDbContext())
            {
                var role = db.Roles.FirstOrDefault(x => x.Id == roleId);
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                manager.RemoveFromRole(userId, role.Name);
                var note = new AspNetUserNote();
                note.EnteredBy = User.Identity.Name;
                note.Note = "Removed user from " + role.Name + " role";

                var user = db.Users.FirstOrDefault(x => x.Id == userId);
                user.Notes.Add(note);
                db.SaveChanges();

            }
        }
开发者ID:humayuns,项目名称:ember-user-admin,代码行数:27,代码来源:MembershipsController.cs

示例6: RemoveUserFromAdmin

        public static void RemoveUserFromAdmin(this string userId)
        {
            ApplicationUser user = db.Users.FirstOrDefault(u => u.Id.Equals(userId));
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            userManager.RemoveFromRole(userId, "Admin");
        }
开发者ID:abigailwest,项目名称:BudgetApp,代码行数:7,代码来源:RoleHelper.cs

示例7: ClearUserRoles

 public void ClearUserRoles(string userId)
 {
     var um = new UserManager<User>(new UserStore<User>(new DatabaseContext()));
     um.UserValidator = new UserValidator<User>(um) { AllowOnlyAlphanumericUserNames = false };
     User user = um.FindById(userId);
     List<IdentityUserRole> currentRoles = user.Roles.ToList();
     currentRoles.ForEach(item => um.RemoveFromRole(user.Id, item.Role.Name));
 }
开发者ID:TDevs,项目名称:2014_TBS,代码行数:8,代码来源:AccountService.cs

示例8: ClearUserRoles

 public void ClearUserRoles(string userId)
 {
     var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new EntityDbContext()));
     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:nongzhsh,项目名称:Nccq,代码行数:11,代码来源:IdentityManager.cs

示例9: UpdateSystemRole

        public JsonResult UpdateSystemRole(string userName, string roleToUpdate)
        {
            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            userMgr.UserValidator = new UserValidator<ApplicationUser>(userMgr) { AllowOnlyAlphanumericUserNames = false };
            var user = userMgr.FindByName(userName);
            userMgr.RemoveFromRole(user.Id, Common.Constants.SYSTEM_ROLE_USER);
            userMgr.RemoveFromRole(user.Id, Common.Constants.SYSTEM_ROLE_ADMINISTRATOR);
            var roleresult = userMgr.AddToRole(user.Id, roleToUpdate);
            if (roleresult.Succeeded)
            {
                return Json(new
                {
                    Status = "OK",
                    Msg = ""
                });
            }
            else
            {
                return Json(new { Status = "ERROR", Message = "Unable to add user to role" });
            }

        }
开发者ID:rajeshwarn,项目名称:Iteedee.BetaDepot,代码行数:22,代码来源:AdministrationController.cs

示例10: ClearUserRoles

 public void ClearUserRoles(string userId)
 {
     var identityDbContext = new ApplicationDbContext();
     var um = new UserManager<ApplicationUser>(
         new UserStore<ApplicationUser>(new ApplicationDbContext()));
     var user = um.FindById(userId);
     var currentRoles = new List<IdentityUserRole>();
     currentRoles.AddRange(user.Roles);
     foreach (var role in currentRoles)
     {
         var _role = identityDbContext.Roles.FirstOrDefault(x=>x.Id == role.RoleId);
         um.RemoveFromRole(userId, _role.Name);
     }
 }
开发者ID:EfeChetirous,项目名称:Efe_SampleInfrastructure,代码行数:14,代码来源:IdentityManagerService.cs

示例11: AddToRole

        public void AddToRole(string role, string employee)
        {
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var user = UserManager.FindByName(employee);
            var userRoles =  user.Roles.ToList();
           
            //user.Roles.Remove(roles);
            foreach (var Role in userRoles)
            {
               var roleName = roleManager.FindById(Role.RoleId);
               UserManager.RemoveFromRole(user.Id, roleName.Name);
            }

            UserManager.AddToRole(user.Id, role);
            
        }
开发者ID:keithh8112,项目名称:ITrack,代码行数:17,代码来源:RoleAssignmentController.cs

示例12: ExcludeUser

        public ActionResult ExcludeUser(string id)
        {
            var userStore = new UserStore<User>(this.dbContext);
            var userManager = new UserManager<User>(userStore);
            try
            {
                userManager.RemoveFromRole(id, GlobalConstants.EngineerRoleName);
            }
            catch (Exception e)
            {
                this.TempData["Error"] = e.Message;
                return this.RedirectToAction("Index");
            }

            this.TempData["Success"] = "User successfully excluded from the role";
            return this.RedirectToAction("Index");
        }
开发者ID:DimitarDKirov,项目名称:ServiceSystem,代码行数:17,代码来源:UsersController.cs

示例13: setRole

        public void setRole(string id, string RoleName)
        {
            var context = new ApplicationDbContext();
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store);
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            var userId = _db.Users.Find(id);
            var roleId = _db.Roles.First(a => a.Name == RoleName);
            if (userId.Roles.Count(a => a.RoleId == roleId.Id) != 0)
            {
                manager.RemoveFromRole(userId.Id, RoleName);
            }
            else
            {
                var roleresult = manager.AddToRole(userId.Id, RoleName);
                //Отправляем письмо, о том, что пользователя авторизовали
                var mail = new MailMessage
                {
                    Subject = "авторизация пройдена",
                    Body = "<p>Уважаемы пользователь " + userId.NickName + "</p>" + "<p>Вы авторизированы на форуме программы Талисман-SQL</p>"
                };

                switch (RoleName)
                {
                    case "user":
                        mail.Body += "<p>Теперь вы можете создавать темы в разделах форума, а также писать сообщения</p>";
                        break;
                    case "moderator":
                        mail.Body += "<p>Теперь вы можете создавать темы в разделах форума, создавать сообщения, закреплять и закрывать темы</p>";
                        break;

                }
                mail.IsBodyHtml = true;
                mail.To.Add(userId.Email);
                Code.Mail.SendEmail(mail);
            }
        }
开发者ID:nigihayami,项目名称:TalismanSqlForum,代码行数:42,代码来源:AdminUserController.cs

示例14: DeleteAdminPermission

        public ActionResult DeleteAdminPermission(string userId)
        {
            List<MoviesLabUser> user = db.Users.ToList();

            if (user == null)
            {
                return RedirectToAction("UserList", new { message = StatusMessage.UserNotFound });
            }

            var userManager = new UserManager<MoviesLabUser>(new UserStore<MoviesLabUser>(db));

            if (userManager.IsInRole(userId, "Администратор"))
            {
                userManager.RemoveFromRole(userId, "Администратор");
                return RedirectToAction("UserList", new { message = StatusMessage.UserDeleteGrantSuccess });
            }
            else
            {
                return RedirectToAction("UserList", new { message = StatusMessage.UserIsntInRole });
            }
        }
开发者ID:solopchenko,项目名称:movieslab,代码行数:21,代码来源:AdminController.cs

示例15: DeleteRoleForUser

 public ActionResult DeleteRoleForUser(string UserName, string RoleName)
 {
     ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
     if (userManager.IsInRole(user.Id, RoleName))
     {
         userManager.RemoveFromRole(user.Id, RoleName);
         ViewBag.ResultMessageDelete = "Role removed from this user successfully";
     }
     else
     {
         ViewBag.ResultMessageDelete = "This user doesn't belong to the selected role";
     }
     // 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();
     var schemeList = db.Schemes.Select(m => new { Value = m.Name, Text = m.Name }).Distinct().ToList();
     ViewBag.Roles = list;
     var listUsers = context.Users.OrderBy(r => r.UserName).ToList().Select(rr => new SelectListItem { Value = rr.UserName.ToString(), Text = rr.UserName }).ToList();
     ViewBag.Users = listUsers;
     ViewBag.SchemeList = new MultiSelectList(schemeList, "Value", "Text");
     return View("Index");
 }
开发者ID:Jaysonh1985,项目名称:CalcEngine,代码行数:22,代码来源:UserRoleMaintenanceController.cs


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