public async Task <ActionResult> Scopes(UserScopeModel model)
        {
            var fullUserStore = UserManager.FullUserStore;
            var adminStore    = new IdentityServer3AdminStore();

            if (model.UserScopeRecords != null)
            {
                // remove the ones that need to be removed
                var queryToBeDeleted = (from item in model.UserScopeRecords
                                        where item.Enabled == false
                                        select item.Name).ToList();
                await adminStore.DeleteScopesByUserIdAsync(model.UserId, queryToBeDeleted);
            }

            var queryToBeAdded = (from item in model.AllowedScopes
                                  where item.Enabled
                                  select item.Name).ToList();

            // add the ones that need to be added.
            if (queryToBeAdded.Any())
            {
                await adminStore.AddScopesToIdentityServerUserAsync(model.UserId, queryToBeAdded);
            }

            return(RedirectToAction("Index"));
        }
        public async Task Test_Create_IdentityServerUser_Add_AND_Delete_AllowedScopes_Async()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var user       = new IdentityServerUser {
                Enabled = true, UserId = userId, UserName = "******" + userId
            };

            await adminStore.CreateIdentityServerUserAsync(user);

            var result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.AreEqual(user.UserId, result.UserId);
            List <string> scopesToAdd = new List <string> {
                "scope1", "scope2", "scope3", "scope4"
            };
            await adminStore.AddScopesToIdentityServerUserAsync(userId, scopesToAdd);

            var scopes = await adminStore.FindScopesByUserAsync(userId);

            Assert.AreEqual(scopes.Count(), scopesToAdd.Count);
            var finalList = scopes.ToList().Except(scopesToAdd);

            Assert.IsFalse(finalList.Any());


            List <string> scopesToDelete = new List <string> {
                "scope1", "scope2"
            };
            await adminStore.DeleteScopesByUserIdAsync(userId, scopesToDelete);

            scopes = await adminStore.FindScopesByUserAsync(userId);

            var finalExpectedList = scopesToAdd.ToList().Except(scopesToDelete);

            Assert.AreEqual(scopes.Count(), finalExpectedList.Count());
            finalList = scopes.ToList().Except(finalExpectedList);
            Assert.IsFalse(finalList.Any());
        }