public async Task Test_Create_IdentityServerUser_Add_ClientIds_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> clientIdsToAdd = new List <string> {
                "clientid1", "clientid2"
            };
            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());
        }
        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 CreateIdentityServerUser(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException("userId");
            }

            var fullUserStore = UserManager.FullUserStore;
            var user          = await fullUserStore.FindByIdAsync(Guid.Parse(userId));

            if (user == null)
            {
                throw new UserDoesNotExitException();
            }
            var adminStore    = new IdentityServer3AdminStore();
            var idsUserExists = await adminStore.FindDoesUserExistByUserIdAsync(userId);

            if (!idsUserExists)
            {
                var claim = new Claim(ClaimTypes.Role, "Developer");
                await fullUserStore.AddClaimAsync(user, claim);

                var idsUser = new IdentityServerUser()
                {
                    Enabled = true, UserId = userId
                };
                await adminStore.CreateIdentityServerUserAsync(idsUser);
            }
        }
        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);
        }
        public async Task Test_Create_IdentityServerUser_Find_No_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);
            var scopes = await adminStore.FindScopesByUserAsync(userId);

            Assert.IsTrue(scopes.Count() == 0);
        }
        public async Task Test_Create_IdentityServerUserAsync()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var user       = new IdentityServerUser {
                Enabled = true, UserId = userId, UserName = "******" + userId
            };
            var result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.IsNull(result);
            await adminStore.CreateIdentityServerUserAsync(user);

            result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.IsNotNull(result);



            Assert.AreEqual(user.UserId, result.UserId);
        }
        public async Task <ActionResult> Manage(IdentityServerUserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var fullUserStore = UserManager.FullUserStore;
            var user          = await fullUserStore.FindByIdAsync(Guid.Parse(model.UserId));

            var claim = new Claim(ClaimTypes.Role, "Developer");
            await fullUserStore.AddClaimAsync(user, claim);

            var adminStore             = new IdentityServer3AdminStore();
            IdentityServerUser idsUser = new IdentityServerUser()
            {
                Enabled = true, UserId = model.UserId
            };
            await adminStore.CreateIdentityServerUserAsync(idsUser);

            return(RedirectToAction("Index"));
        }