示例#1
0
        public async Task <ActionResult <IdentityUserRole <string> > > CreateUserRole(string id, string rid)
        {
            try
            {
                IdentityUserRole <string> userRole = await _context.CreateUserRole(_userManager, id, rid);

                return(CreatedAtRoute(nameof(GetUserRole), new { id = id, rid = rid }, userRole));
            }
            catch (ControllerNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (ControllerBadRequestException e)
            {
                return(BadRequest(e.Message));
            }
            catch (ControllerUnauthorizedException)
            {
                return(new UnauthorizedResult());
            }
        }
示例#2
0
        public async Task <IActionResult> EditRoles(EditRolesModel roleadd)
        {
            try
            {
                CovenantUser user = await _context.GetUserByUsername(roleadd.UserName);

                IEnumerable <string> userRoles = (await _context.GetUserRoles(user.Id)).Select(UR =>
                {
                    Task <IdentityRole> t = _context.GetRole(UR.RoleId);
                    t.Wait();
                    return(t.Result.Name);
                });

                IEnumerable <string> userRolesRemain = userRoles.Where(UR => roleadd.Rolenames.Contains(UR));
                foreach (string rolename in roleadd.Rolenames)
                {
                    // Selected role that has not been added, must add
                    if (!userRolesRemain.Contains(rolename))
                    {
                        IdentityRole role = await _context.GetRoleByName(rolename);

                        await _context.CreateUserRole(_userManager, user.Id, role.Id);
                    }
                }

                IEnumerable <string> userRolesNotRemain = userRoles.Where(UR => !roleadd.Rolenames.Contains(UR));
                foreach (string rolename in userRolesNotRemain)
                {
                    // Did not select role that is already added, must remove
                    IdentityRole role = await _context.GetRoleByName(rolename);

                    await _context.DeleteUserRole(_userManager, user.Id, role.Id);
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
            {
                return(RedirectToAction(nameof(Index)));
            }
        }