public async Task Test_Create_IdentityServerUser_Add_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"
            };
            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());
        }
        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_Add_AllowedScopes_To_NonExisting_User_Async()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();

            List <string> scopesToAdd = new List <string> {
                "scope1", "scope2"
            };
            var appliedInfo = await adminStore.AddScopesToIdentityServerUserAsync(userId, scopesToAdd);

            Assert.IsFalse(appliedInfo.Applied);
            Assert.IsNotNull(appliedInfo.Exception);
            Assert.IsNotNull(appliedInfo.Exception as UserDoesNotExitException);
        }
        public async Task Test_Create_And_Delete_IdentityServerUserAsync()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var user       = new IdentityServerUser {
                Enabled = true, UserId = userId, UserName = "******" + userId
            };

            var appliedInfo = await adminStore.CreateIdentityServerUserAsync(user);

            Assert.IsTrue(appliedInfo.Applied);
            Assert.IsNull(appliedInfo.Exception);
            var result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.AreEqual(user.UserId, result.UserId);

            List <string> clientIdsToAdd = new List <string> {
                "clientid1", "clientid2", "clientid3", "clientid4"
            };
            await adminStore.AddClientIdToIdentityServerUserAsync(userId, clientIdsToAdd);

            var clientIds = await adminStore.FindClientIdsByUserAsync(userId);

            Assert.AreEqual(clientIds.Count(), clientIdsToAdd.Count);
            var finalList = clientIds.ToList().Except(clientIdsToAdd);

            Assert.IsFalse(finalList.Any());

            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);
            finalList = scopes.ToList().Except(scopesToAdd);
            Assert.IsFalse(finalList.Any());

            appliedInfo = await adminStore.DeleteIdentityServerUserAsync(userId);

            Assert.IsTrue(appliedInfo.Applied);
            Assert.IsNull(appliedInfo.Exception);
            result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.IsNull(result);
        }