public async Task GetClaimsForUser()
        {
            // Create a session and user store for this test.
            var session   = SessionFactory.OpenSession();
            var userStore = new TestUserStore(session);
            // Create and save a user with some claims.
            var user = new TestUser {
                UserName = "******"
            };
            int numberOfClaims = 5;
            var claimType      = ClaimTypes.Role;
            var claimValue     = "Admin_GetClaimsForUserTest";
            var claim          = new Claim(claimType, claimValue);

            using (var transaction = session.BeginTransaction())
            {
                await userStore.CreateAsync(user);

                await userStore.AddClaimsAsync(user, new[] { claim });

                for (int i = 0; i < numberOfClaims - 1; i++)
                {
                    var loopClaim = new Claim(claimType, "Admin_GetClaimsForUserTest_" + i);
                    await userStore.AddClaimsAsync(user, new[] { loopClaim });
                }
                transaction.Commit();
            }
            // Check the user has an id and the claims.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Claims.Count, numberOfClaims);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user.
            TestUser loadUser;

            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                transaction.Commit();
            }
            // Check we have the same user and it has the claims.
            Assert.AreEqual(loadUser.Id, user.Id);
            Assert.AreEqual(loadUser.Claims.Count, numberOfClaims);
            var userClaims = await userStore.GetClaimsAsync(loadUser);

            var userClaim = userClaims.SingleOrDefault(c => c.Type == claimType && c.Value == claimValue);

            Assert.IsNotNull(userClaim);
        }
コード例 #2
0
ファイル: UserStoreTests.cs プロジェクト: MatthewRudolph/Airy
        public async Task GetClaimsForUser()
        {
            // Create a session and user store for this test.
            var session = SessionFactory.OpenSession();
            var userStore = new TestUserStore<TestUser>(session);
            // Create and save a user with some claims.
            var user = new TestUser { UserName = "******" };
            int numberOfClaims = 5;
            var claimType = ClaimTypes.Role;
            var claimValue = "Admin_GetClaimsForUserTest";
            var claim = new Claim(claimType, claimValue);
            using (var transaction = session.BeginTransaction())
            {
                await userStore.CreateAsync(user);
                await userStore.AddClaimAsync(user, claim);
                for (int i = 0; i < numberOfClaims - 1; i++)
                {
                    var loopClaim = new Claim(claimType, "Admin_GetClaimsForUserTest_" + i);
                    await userStore.AddClaimAsync(user, loopClaim);
                }
                transaction.Commit();
            }
            // Check the user has an id and the claims.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Claims.Count, numberOfClaims);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user.
            TestUser loadUser;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                transaction.Commit();
            }
            // Check we have the same user and it has the claims.
            Assert.AreEqual(loadUser.Id, user.Id);
            Assert.AreEqual(loadUser.Claims.Count, numberOfClaims);
            var userClaims = await userStore.GetClaimsAsync(loadUser);
            var userClaim = userClaims.SingleOrDefault(c => c.Type == claimType && c.Value == claimValue);
            Assert.IsNotNull(userClaim);
        }