示例#1
0
        public async Task RemoveClaimForUser()
        {
            // 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 a claim.
            var user = new TestUser { UserName = "******" };
            var claimType = ClaimTypes.Role;
            var claimValue = "Admin_RemoveClaimForUserTest";
            var claim = new Claim(claimType, claimValue);
            using (var transaction = session.BeginTransaction())
            {
                await userStore.CreateAsync(user);
                await userStore.AddClaimAsync(user, claim);
                transaction.Commit();
            }
            // Check the user has an id and the claim.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Claims.Count, 1);
            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 and remove the claim.
            TestUser loadUser;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                await userStore.RemoveClaimAsync(loadUser, claim);
                transaction.Commit();
            }
            // Check we have the same user and it now has no claims.
            Assert.AreEqual(loadUser.Id, user.Id);
            Assert.AreEqual(loadUser.Claims.Count, 0);
        }