Exemplo n.º 1
0
        // GET: Account/Remove/5
        public async Task <IActionResult> Remove()
        {
            UserAndRolesHelper userAndRoleHelper = new UserAndRolesHelper();

            userAndRoleHelper.ThirdTable = _roleManager.Roles.ToList();
            IdentityRole adminRole = new IdentityRole();

            foreach (var item in userAndRoleHelper.ThirdTable)
            {
                if (item.Name == "Admin")
                {
                    adminRole = item;
                    userAndRoleHelper.ThirdTable.Remove(adminRole);
                    break;
                }
            }
            return(View(userAndRoleHelper));
        }
        // GET: ApplicationUsers
        public async Task <IActionResult> Index()
        {
            UserAndRolesHelper viewModel = new UserAndRolesHelper();

            viewModel.FirstTable = _context.Users.ToList();
            var users        = _context.Users.ToList();
            var roles        = _context.Roles.ToList();
            var userAndRoles = _context.UserRoles.ToList();

            List <KeyValuePair <String, String> > ListOfUsersAndRoles = new List <KeyValuePair <String, String> >();

            foreach (var userAndRole in userAndRoles)
            {
                String username = "";
                String roleName = "";
                foreach (var user in users)
                {
                    if (userAndRole.UserId == user.Id)
                    {
                        username = user.UserName;
                        break;
                    }
                }
                foreach (var role in roles)
                {
                    if (userAndRole.RoleId == role.Id)
                    {
                        roleName = role.Name;
                    }
                }
                var element = new KeyValuePair <String, String>(username, roleName);
                ListOfUsersAndRoles.Add(element);
            }

            viewModel.SecondTable = ListOfUsersAndRoles;
            viewModel.ThirdTable  = roles;
            return(View(viewModel));
        }