コード例 #1
0
        public async Task <IHttpActionResult> AddUserToRole(UserRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userId   = model.UserId;
            string roleName = model.RoleName;

            ApplicationUser user = UserManager.FindById(userId);

            if (user == null)
            {
                return(BadRequest("The user id does not exist: \"" + userId + "\""));
            }

            IdentityRole role = new IdentityRole(roleName);

            if (!AppRoleManager.RoleExists(roleName))
            {
                IdentityResult result = await AppRoleManager.CreateAsync(role);

                if (!result.Succeeded)
                {
                    return(GetErrorResult(result));
                }
            }

            UserManager.AddToRole(user.Id, roleName);

            return(Ok());
        }
コード例 #2
0
        public IHttpActionResult Add(UserRoleBindingModel model)
        {
            try
            {
                var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db));
                var roleManager = new RoleManager <ApplicationRole>(new RoleStore <ApplicationRole>(db));

                var role = roleManager.FindById(model.RoleId);

                if (role == null)
                {
                    return(BadRequest("Role does not exists"));
                }

                if (userManager.IsInRole(model.UserId, role.Name))
                {
                    return(BadRequest("User already assigned to this role"));
                }

                userManager.AddToRole(model.UserId, role.Name);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
コード例 #3
0
        public async Task <IActionResult> ChangeRole(UserRoleBindingModel rolesModel)
        {
            // finds the user and deletes his current role, afterwards sets the new role.
            // only one role is allowed by user.
            await this.roleService.ChangeUserRole(rolesModel.Username, rolesModel.Role);

            return(this.RedirectToAction("Index"));
        }
コード例 #4
0
        public ActionResult AddUserToRole(UserRoleBindingModel Model)
        {
            var postTask = ApiHelper.ApiClient.PostAsJsonAsync <UserRoleBindingModel>("api/Role/AddUserToRole", Model);

            postTask.Wait();

            var result = postTask.Result;

            if (result.IsSuccessStatusCode)
            {
                return(RedirectToAction("Roles"));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
                return(View());
            }
        }
コード例 #5
0
        public ActionResult RemoveUserFromRole(UserRoleBindingModel Model)
        {
            //var parameters = new Dictionary<string, string> { { "id", Model.UserId }, { "RoleName", Model.RoleName } };
            //var encodedContent = new FormUrlEncodedContent(parameters);
            var postTask = ApiHelper.ApiClient.PostAsJsonAsync <UserRoleBindingModel>("api/Role/RemoveUserFromRole", Model);

            postTask.Wait();

            var result = postTask.Result;

            if (result.IsSuccessStatusCode)
            {
                return(RedirectToAction("Roles"));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
                return(View());
            }
        }
コード例 #6
0
        public async Task <IHttpActionResult> RemoveUserFromRole(UserRoleBindingModel Model)
        {
            var User = RoleContext.Users.SingleOrDefault(u => u.Id == Model.UserId);

            if (User == null || User.IsDeleted == true)
            {
                return(NotFound());
            }
            var Rolestore   = new RoleStore <IdentityRole>(new ApplicationDbContext());
            var roleManager = new RoleManager <IdentityRole>(Rolestore);
            var Role        = await roleManager.FindByNameAsync(Model.RoleName);

            if (Role == null)
            {
                return(NotFound());
            }
            await UserManager.RemoveFromRoleAsync(User.Id, Model.RoleName);

            RoleContext.SaveChanges();
            return(Ok());
        }
コード例 #7
0
        public IHttpActionResult DeleteUserRole(UserRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userId   = model.UserId;
            string roleName = model.RoleName;

            ApplicationUser user = UserManager.FindById(userId);

            if (user == null)
            {
                return(BadRequest("The user id does not exist: \"" + userId + "\""));
            }

            UserManager.RemoveFromRole(user.Id, roleName);

            return(Ok());
        }