コード例 #1
0
        public async Task <IActionResult> ChangeRole([FromBody] UsersRequestModel usersRequest)
        {
            var user = await _userManager.FindByIdAsync(usersRequest.Id);

            if (await _userManager.IsInRoleAsync(user, "admin"))
            {
                await _userManager.AddToRoleAsync(user, "user");

                await _userManager.RemoveFromRoleAsync(user, "admin");
            }
            else
            {
                await _userManager.AddToRoleAsync(user, "admin");

                await _userManager.RemoveFromRoleAsync(user, "user");
            }

            var admins = await _userManager.GetUsersInRoleAsync("admin");

            return(StatusCode(200, _userManager.Users.AsEnumerable().Select(user =>
                                                                            new UsersResponseModel()
            {
                Id = user.Id,
                Name = user.UserName,
                Email = user.Email,
                Role = admins.Any(admin => admin.UserName == user.UserName) ? "admin" : "user",
                IsBanned = user.IsBanned
            }
                                                                            )));
        }
コード例 #2
0
        public async Task <IActionResult> DeleteUser([FromBody] UsersRequestModel usersRequest)
        {
            await _userManager.DeleteAsync(await _userManager.FindByIdAsync(usersRequest.Id));

            var admins = await _userManager.GetUsersInRoleAsync("admin");

            return(StatusCode(200, _userManager.Users.AsEnumerable().Select(user =>
                                                                            new UsersResponseModel()
            {
                Id = user.Id,
                Name = user.UserName,
                Email = user.Email,
                Role = admins.Any(admin => admin.UserName == user.UserName) ? "admin" : "user",
                IsBanned = user.IsBanned
            }
                                                                            )));
        }