Example #1
0
            public async Task Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value()
            {
                // arrange
                var claim1         = new Claim("ClaimType", "some value");
                var claim2         = new Claim(claim1.Type, "some other value");
                var identityClaim1 = new IdentityClaim {
                    ClaimType = claim1.Type, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                var role = new IdentityRole("Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value");

                role.Claims.Add(identityClaim1);
                role.Claims.Add(identityClaim2);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.RemoveClaimAsync(role, claim1);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, roleFromDb.Claims);
            }
Example #2
0
        public static void Equal(IdentityClaim expected, IdentityClaim actual)
        {
            Assert.True((expected == null && actual == null) || (expected != null && actual != null));

            Assert.Equal(expected.ClaimType, actual.ClaimType);
            Assert.Equal(expected.ClaimValue, actual.ClaimValue);
        }
 private void OnClaimValueChanged(IdentityClaim claim)
 {
     Model.IdentityClaims.Add(claim);
     _claim   = new IdentityClaim();
     claim.Id = Guid.NewGuid().ToString();
     HandleModificationState.EntityCreated(claim);
 }
Example #4
0
        public void UpdateIdentityResource(int id, IdentityResourceDto identityResourceDto)
        {
            var transaction = this.Session.BeginTransaction();

            try
            {
                var identityResource = this.Session.Get <IdentityResource>(id);
                identityResource = identityResourceDto.ToEntity(identityResource);
                this.Session.Update(identityResource);
                this.Session.CreateQuery("delete from IdentityClaim where IdentityResourceId=:IdentityResourceId")
                .SetInt32("IdentityResourceId", id)
                .ExecuteUpdate();
                identityResourceDto.UserClaims.ForEach(claimType =>
                {
                    IdentityClaim identityClaim      = new IdentityClaim();
                    identityClaim.IdentityResourceId = identityResource.Id;
                    identityClaim.Type = claimType;
                    this.Session.Save(identityClaim);
                });
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
        }
Example #5
0
            public async Task Adding_existing_claim_to_role_does_not_update_database_role_record()
            {
                // arrange
                var claim = new Claim("ClaimType", "some value");

                var role          = new IdentityRole("Adding_existing_claim_to_role_does_not_update_database_role_record");
                var identityClaim = new IdentityClaim {
                    ClaimType = claim.Type, ClaimValue = claim.Value
                };

                role.Claims.Add(identityClaim);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.AddClaimAsync(role, claim);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, roleFromDb.Claims);
            }
Example #6
0
        public virtual async Task <IdentityResult> RemoveClaimAsync(TUser user, IdentityClaim claim)
        {
            this.ThrowIfDisposed();
            await StoreEx.RemoveClaimAsync(user, claim);

            return(new IdentityResult(new string[] { }));
        }
Example #7
0
            public async Task Returns_all_claims_from_users_claims_and_roles()
            {
                // arrange
                var user   = new IdentityUser();
                var claim1 = new IdentityClaim {
                    ClaimType = "ClaimType1", ClaimValue = "some value"
                };
                var claim2 = new IdentityClaim {
                    ClaimType = "ClaimType2", ClaimValue = "some other value"
                };
                var claim3 = new IdentityClaim {
                    ClaimType = "ClaimType3", ClaimValue = "yet another value"
                };

                user.Claims.Add(claim1);
                user.Claims.Add(claim2);

                var role = new IdentityRole();

                role.Claims.Add(claim1);
                role.Claims.Add(claim3);
                user.Roles.Add(role);

                // act
                var result = await _userStore.GetClaimsAsync(user);

                // assert
                Assert.Equal(3, result.Count);
                Assert.True(result.Single(c => c.Type == claim1.ClaimType && c.Value == claim1.ClaimValue) != null);
                Assert.True(result.Single(c => c.Type == claim2.ClaimType && c.Value == claim2.ClaimValue) != null);
                Assert.True(result.Single(c => c.Type == claim3.ClaimType && c.Value == claim3.ClaimValue) != null);
            }
		public static void Equal(IdentityClaim expected, IdentityClaim actual)
		{
			Assert.True((expected == null && actual == null) || (expected != null && actual != null));

			Assert.Equal(expected.ClaimType, actual.ClaimType);
			Assert.Equal(expected.ClaimValue, actual.ClaimValue);
		}
		public UserStoreTests(string collectionPrefix)
		{
			collectionPrefix = $"{typeof(UserStoreTests).Name}_{collectionPrefix}";

			_databaseFixture = new DatabaseFixture(collectionPrefix);
			_userCollection = _databaseFixture.GetCollection<IdentityUser>();
			_roleCollection = _databaseFixture.GetCollection<IdentityRole>();
			_databaseContext = new IdentityDatabaseContext { UserCollection = _userCollection, RoleCollection = _roleCollection };

			_errorDescriber = new IdentityErrorDescriber();
			_userStore = new UserStore<IdentityUser, IdentityRole>(_databaseContext, null, _errorDescriber);


			_claim1 = new Claim("ClaimType1", "some value");
			_claim2 = new Claim("ClaimType2", "some other value");
			_claim3 = new Claim("other type", "some other value");

			_claim1SameType = new Claim(_claim1.Type, _claim1.Value + " different");

			_identityClaim1 = new IdentityClaim(_claim1);
			_identityClaim2 = new IdentityClaim(_claim2);
			_identityClaim3 = new IdentityClaim(_claim3);

			_identityClaim1SameType = new IdentityClaim(_claim1SameType);
		}
Example #10
0
        private bool IsUniqueClaim(IdentityClaim identityClaim)
        {
            var query       = Query.And(Query.EQ("name", identityClaim.Name), Query.Not(Query.EQ("_id", identityClaim.Id)));
            var hasRegister = DbContext.ClainsCollection.FindOneAs <IdentityClaim>(query);

            return(hasRegister == null);
        }
Example #11
0
            public async Task Update_role_returns_Success()
            {
                // arrange
                var claim1 = new IdentityClaim {
                    ClaimType = "ClaimType1", ClaimValue = "some value"
                };
                var claim2 = new IdentityClaim {
                    ClaimType = "ClaimType2", ClaimValue = "some other value"
                };
                var role = new IdentityRole("Update_role_returns_Success");

                role.Claims.Add(claim1);

                // initial role creation
                await _roleStore.CreateAsync(role);

                role.Name = role.Name + " different";
                role.Claims.Add(claim2);


                // act
                var result = await _roleStore.UpdateAsync(role);

                // assert
                IdentityResultAssert.IsSuccess(result);

                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityRoleAssert.Equal(role, roleFromDb);
            }
Example #12
0
            public async Task Removing_unknown_claim_does_not_change_database_role_record()
            {
                // arrange
                var identityClaim = new IdentityClaim {
                    ClaimType = "claim type", ClaimValue = "some value"
                };
                var role = new IdentityRole("Removing_unknown_claim_does_not_change_database_role_record");

                role.Claims.Add(identityClaim);

                await _roleStore.CreateAsync(role);

                // act
                var claim = new Claim("other type", "some other value");
                await _roleStore.RemoveClaimAsync(role, claim);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, roleFromDb.Claims);
            }
Example #13
0
            public async Task Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database()
            {
                // arrange
                var claim1 = new Claim("ClaimType", "some value");
                var claim2 = new Claim(claim1.Type, "some other value");

                var role = new IdentityRole("Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database");
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.AddClaimAsync(role, claim1);

                await _roleStore.AddClaimAsync(role, claim2);

                // assert
                var identityClaim1 = new IdentityClaim {
                    ClaimType = claim1.Type, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim1, identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim1, identityClaim2
                }, roleFromDb.Claims);
            }
        public UserStoreTests(string collectionPrefix)
        {
            collectionPrefix = $"{typeof(UserStoreTests).Name}_{collectionPrefix}";

            _databaseFixture = new DatabaseFixture(collectionPrefix);
            _userCollection  = _databaseFixture.GetCollection <IdentityUser>();
            _roleCollection  = _databaseFixture.GetCollection <IdentityRole>();
            _databaseContext = new IdentityDatabaseContext {
                UserCollection = _userCollection, RoleCollection = _roleCollection
            };

            _errorDescriber = new IdentityErrorDescriber();
            _userStore      = new UserStore <IdentityUser, IdentityRole>(_databaseContext, null, _errorDescriber);


            _claim1 = new Claim("ClaimType1", "some value");
            _claim2 = new Claim("ClaimType2", "some other value");
            _claim3 = new Claim("other type", "some other value");

            _claim1SameType = new Claim(_claim1.Type, _claim1.Value + " different");

            _identityClaim1 = new IdentityClaim(_claim1);
            _identityClaim2 = new IdentityClaim(_claim2);
            _identityClaim3 = new IdentityClaim(_claim3);

            _identityClaim1SameType = new IdentityClaim(_claim1SameType);
        }
Example #15
0
            public async Task Remove_existing_claim_updates_database_role_record()
            {
                // arrange
                var claim1         = new Claim("ClaimType", "some value");
                var claim2         = new Claim("ClaimType2", "some other value");
                var identityClaim1 = new IdentityClaim {
                    ClaimType = claim1.Type, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                var role = new IdentityRole("Remove_existing_claim_updates_database_role_record");

                role.Claims.Add(identityClaim1);
                role.Claims.Add(identityClaim2);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.RemoveClaimAsync(role, claim1);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, roleFromDb.Claims);
            }
        public UserStoreTests(string collectionPrefix)
        {
            _databaseFixture  = new DatabaseFixture(collectionPrefix);
            _userCollection   = _databaseFixture.GetCollection <IdentityUser>();
            _collectionPrefix = collectionPrefix;

            _errorDescriber = new IdentityErrorDescriber();
            _userStore      = new UserStore <IdentityUser, IdentityRole>(_userCollection, _errorDescriber);


            _claim1 = new Claim("ClaimType1", "some value");
            _claim2 = new Claim("ClaimType2", "some other value");
            _claim3 = new Claim("other type", "some other value");

            _claim1SameType = new Claim(_claim1.Type, _claim1.Value + " different");

            _identityClaim1 = new IdentityClaim {
                ClaimType = _claim1.Type, ClaimValue = _claim1.Value
            };
            _identityClaim2 = new IdentityClaim {
                ClaimType = _claim2.Type, ClaimValue = _claim2.Value
            };
            _identityClaim3 = new IdentityClaim {
                ClaimType = _claim3.Type, ClaimValue = _claim3.Value
            };

            _identityClaim1SameType = new IdentityClaim {
                ClaimType = _claim1SameType.Type, ClaimValue = _claim1SameType.Value
            };
        }
        public IdentityUserTests()
        {
            User = new IdentityUser("John Doe");

            Role1 = new IdentityRole {
                Name = "Role1"
            };
            Role2 = new IdentityRole {
                Name = "Role2"
            };

            Claim1 = new IdentityClaim {
                ClaimType = "Claim1", ClaimValue = "Some value"
            };
            Claim2 = new IdentityClaim {
                ClaimType = "Claim2", ClaimValue = "Some other value"
            };
            Claim3 = new IdentityClaim {
                ClaimType = "Claim3", ClaimValue = "Yet another value"
            };
            Claim4 = new IdentityClaim {
                ClaimType = "Claim4", ClaimValue = "Many many claims"
            };

            Claim1Alt = new IdentityClaim {
                ClaimType = "Claim1", ClaimValue = "Some alternate value"
            };

            Login1 = new UserLoginInfo("SomeProvider", "AKey", "Bob");
        }
        public IdentityUserTests()
        {
            User = new IdentityUser("John Doe");

            Role1 = new IdentityRole {
                Name = "Role1"
            };
            Role2 = new IdentityRole {
                Name = "Role2"
            };

            Claim1 = new IdentityClaim {
                ClaimType = "Claim1", ClaimValue = "Some value"
            };
            Claim2 = new IdentityClaim {
                ClaimType = "Claim2", ClaimValue = "Some other value"
            };
            Claim3 = new IdentityClaim {
                ClaimType = "Claim3", ClaimValue = "Yet another value"
            };
            Claim4 = new IdentityClaim {
                ClaimType = "Claim4", ClaimValue = "Many many claims"
            };

            Claim1Alt = new IdentityClaim {
                ClaimType = "Claim1", ClaimValue = "Some alternate value"
            };

            Login1 = new IdentityUserLogin {
                LoginProvider = "Form", ProviderDisplayName = "Bob", ProviderKey = "password"
            };
        }
Example #19
0
            public async Task Adding_new_claim_to_role_with_null_claims_updates_database_role_record()
            {
                // arrange
                var claim = new Claim("ClaimType", "some value");

                var role = new IdentityRole("Adding_new_claim_to_role_with_null_claims_updates_database_role_record");

                role.Claims = null;

                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.AddClaimAsync(role, claim);

                // assert

                // check role claims from memory
                var identityClaim = new IdentityClaim(claim);

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, roleFromDb.Claims);
            }
Example #20
0
            public void Init_with_empty_constructor_creates_with_null_CliamType_and_ClaimValue()
            {
                // act
                var result = new IdentityClaim();

                // assert
                Assert.Null(result.ClaimType);
                Assert.Null(result.ClaimValue);
            }
Example #21
0
 public IdentityClaimTests()
 {
     _identityClaim1 = new IdentityClaim {
         ClaimType = "ClaimType1", ClaimValue = "some value"
     };
     _identityClaim1SameType = new IdentityClaim {
         ClaimType = _identityClaim1.ClaimType, ClaimValue = _identityClaim1.ClaimValue + " different"
     };
 }
			public void Init_with_empty_constructor_creates_with_null_CliamType_and_ClaimValue()
			{
				// act
				var result = new IdentityClaim();

				// assert
				Assert.Null(result.ClaimType);
				Assert.Null(result.ClaimValue);
			}
        public void Create_FromClaim_SetsTypeAndValue()
        {
            var claim = new Claim("type", "value");

            var userClaim = new IdentityClaim(claim);

            Assert.Equal("type", userClaim.Type);
            Assert.Equal("value", userClaim.Value);
        }
Example #24
0
            public void When_claims_dont_match_returns_false()
            {
                // arrange
                var c = new IdentityClaim {
                    ClaimType = _identityClaim1SameType.ClaimType, ClaimValue = _identityClaim1SameType.ClaimValue
                };;


                // assert
                Assert.False(_identityClaim1.Equals(c));
            }
        public void ToSecurityClaim_SetsTypeAndValue()
        {
            var userClaim = new IdentityClaim {
                Type = "t", Value = "v"
            };

            var claim = userClaim.ToSecurityClaim();

            Assert.Equal("t", claim.Type);
            Assert.Equal("v", claim.Value);
        }
Example #26
0
            public void When_claims_match_returns_true()
            {
                // arrange
                var c = new IdentityClaim {
                    ClaimType = _identityClaim1.ClaimType, ClaimValue = _identityClaim1.ClaimValue
                };;


                // assert
                Assert.True(_identityClaim1.Equals(c));
            }
			public void Init_from_Claim_object_creates_with_CliamType_and_ClaimValue_set_to_values_from_Claim()
			{
				// arrange
				var claim = new Claim("ClaimType", "some value here");

				// act
				var result = new IdentityClaim(claim);

				// assert
				Assert.Equal(claim.Type, result.ClaimType);
				Assert.Equal(claim.Value, result.ClaimValue);
			}
		public IdentityRoleTests()
		{
			Role = new IdentityRole { Name = "Role" };

			Claim1 = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some value" };
			Claim2 = new IdentityClaim { ClaimType = "Claim2", ClaimValue = "Some other value" };
			Claim3 = new IdentityClaim { ClaimType = "Claim3", ClaimValue = "Yet another value" };
			Claim4 = new IdentityClaim { ClaimType = "Claim4", ClaimValue = "Many many claims" };

			Claim1Alt = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some alternate value" };

		}
Example #29
0
            public void Init_from_Claim_object_creates_with_CliamType_and_ClaimValue_set_to_values_from_Claim()
            {
                // arrange
                var claim = new Claim("ClaimType", "some value here");

                // act
                var result = new IdentityClaim(claim);

                // assert
                Assert.Equal(claim.Type, result.ClaimType);
                Assert.Equal(claim.Value, result.ClaimValue);
            }
			public void Init_string_values_creates_CliamType_and_ClaimValue_set_to_values()
			{
				// arrange
				string type = "ClaimType";
				string val = " some value here";

				// act
				var result = new IdentityClaim(type, val);

				// assert
				Assert.Equal(type, result.ClaimType);
				Assert.Equal(val, result.ClaimValue);
			}
Example #31
0
            public void Init_string_values_creates_CliamType_and_ClaimValue_set_to_values()
            {
                // arrange
                string type = "ClaimType";
                string val  = " some value here";

                // act
                var result = new IdentityClaim(type, val);

                // assert
                Assert.Equal(type, result.ClaimType);
                Assert.Equal(val, result.ClaimValue);
            }
 public int Delete(IdentityClaim claim, IdentityUser user)
 {
     return
         (_database.Execute(
              "DELETE FROM \r\n" +
              "   UserClaim \r\n" +
              "WHERE \r\n" +
              "   UserId = @0 \r\n" +
              "   AND ClaimId = @1",
              user.Id,
              claim.Id
              ));
 }
        /// <summary>
        /// TODO: --> Add to UserManagerEx/Extensions
        /// Deletes the assigment of a special (or global) claim assigment
        /// </summary>
        /// <param name="user"></param>
        /// <param name="claim"></param>
        /// <returns></returns>
        public Task RemoveClaimAsync(TUser user, IdentityClaim claim)
        {
            if (user == null)
            {
                throw new ArgumentNullException(IdentityConstants.User);
            }

            if (claim == null)
            {
                throw new ArgumentNullException(IdentityConstants.Claim);
            }

            _userClaimTable.Delete(claim, user);

            return(Task.FromResult <object>(null));
        }
        /// <summary>
        /// TODO: --> Add to UserManagerEx/Extensions
        /// Adds a client specitic (or global) claim to the user
        /// </summary>
        /// <param name="user"></param>
        /// <param name="claim"></param>
        /// <returns></returns>
        public Task AddClaimAsync(TUser user, IdentityClaim claim)
        {
            if (user == null)
            {
                throw new ArgumentNullException(IdentityConstants.User);
            }

            if (claim == null)
            {
                throw new ArgumentNullException(IdentityConstants.Claim);
            }

            _userClaimTable.Insert(claim.Id, user.Id);

            return(Task.FromResult <object>(null));
        }
        public static Identity WithClaim(this Identity identity, string type, string value, bool isIdentifyingClaim, bool isServerSideOnly = false)
        {
            if (identity.Claims.ContainsKey(type))
            {
                var claim = identity.Claims[type];
                claim.Value = value;
                claim.IsIdentifyingClaim = isIdentifyingClaim;
                claim.IsServerSideOnly   = isServerSideOnly;
            }
            else
            {
                var claim = new IdentityClaim(value, isIdentifyingClaim, isServerSideOnly);
                identity.Claims.Add(type, claim);
            }

            return(identity);
        }
		public IdentityUserTests()
		{
			User = new IdentityUser("John Doe");

			Role1 = new IdentityRole { Name = "Role1" };
			Role2 = new IdentityRole { Name = "Role2" };

			Claim1 = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some value" };
			Claim2 = new IdentityClaim { ClaimType = "Claim2", ClaimValue = "Some other value" };
			Claim3 = new IdentityClaim { ClaimType = "Claim3", ClaimValue = "Yet another value" };
			Claim4 = new IdentityClaim { ClaimType = "Claim4", ClaimValue = "Many many claims" };

			Claim1Alt = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some alternate value" };

			Login1 = new UserLoginInfo("SomeProvider", "AKey", "Bob");

		}
		public IdentityUserTests()
		{
			User = new IdentityUser("John Doe");

			Role1 = new IdentityRole { Name = "Role1" };
			Role2 = new IdentityRole { Name = "Role2" };

			Claim1 = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some value" };
			Claim2 = new IdentityClaim { ClaimType = "Claim2", ClaimValue = "Some other value" };
			Claim3 = new IdentityClaim { ClaimType = "Claim3", ClaimValue = "Yet another value" };
			Claim4 = new IdentityClaim { ClaimType = "Claim4", ClaimValue = "Many many claims" };

			Claim1Alt = new IdentityClaim { ClaimType = "Claim1", ClaimValue = "Some alternate value" };

			Login1 = new IdentityUserLogin { LoginProvider = "Form", ProviderDisplayName = "Bob", ProviderKey = "password"};

		}
			public async Task Create_role_returns_Success()
			{
				// arrange
				var claim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "some value" };
				var claim2 = new IdentityClaim { ClaimType = "ClaimType2", ClaimValue = "some other value" };
				var role = new IdentityRole("Create_role_returns_Success");
				role.Claims.Add(claim1);
				role.Claims.Add(claim2);

				// act
				var result = await _roleStore.CreateAsync(role);

				// assert
				IdentityResultAssert.IsSuccess(result);

				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityRoleAssert.Equal(role, roleFromDb);
			}
Example #39
0
        public JsonResult UpdateAllowedExclusionCount(string id, int count)
        {
            string claimType = "http://21brews.com/identity/claims/allowed-exclusions";

            IdentityClaim claim = DAL.Context.Claims.FirstOrDefault(x => x.UserId == id && x.Type == claimType);

            if (claim == null)
            {
                claim = new IdentityClaim {
                    UserId = id, Type = claimType, Value = "2"
                };
                DAL.Context.Claims.Add(claim);
            }

            claim.Value = count.ToString();
            DAL.Context.SaveChanges();

            return(Json(new { Success = true }));
        }
			public async Task Adding_existing_claim_to_role_does_not_update_database_role_record()
			{
				// arrange
				var claim = new Claim("ClaimType", "some value");

				var role = new IdentityRole("Adding_existing_claim_to_role_does_not_update_database_role_record");
				var identityClaim = new IdentityClaim(claim);
				role.Claims.Add(identityClaim);
				await _roleStore.CreateAsync(role);

				// act
				await _roleStore.AddClaimAsync(role, claim);

				// assert

				// check role claims from memory
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim }, role.Claims);

				// check role claims from DB
				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim }, roleFromDb.Claims);
			}
			public async Task Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database()
			{
				// arrange
				var claim1 = new Claim("ClaimType", "some value");
				var claim2 = new Claim(claim1.Type, "some other value");

				var role = new IdentityRole("Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database");
				await _roleStore.CreateAsync(role);

				// act
				await _roleStore.AddClaimAsync(role, claim1);
				await _roleStore.AddClaimAsync(role, claim2);

				// assert
				var identityClaim1 = new IdentityClaim(claim1);
				var identityClaim2 = new IdentityClaim(claim2);

				// check role claims from memory
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim1, identityClaim2 }, role.Claims);

				// check role claims from DB
				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim1, identityClaim2 }, roleFromDb.Claims);
			}
			public async Task Removing_unknown_claim_does_not_change_database_role_record()
			{
				// arrange
				var identityClaim = new IdentityClaim { ClaimType = "claim type", ClaimValue = "some value" };
				var role = new IdentityRole("Removing_unknown_claim_does_not_change_database_role_record");
				role.Claims.Add(identityClaim);

				await _roleStore.CreateAsync(role);

				// act
				var claim = new Claim("other type", "some other value");
				await _roleStore.RemoveClaimAsync(role, claim);

				// assert

				// check role claims from memory
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim }, role.Claims);

				// check role claims from DB
				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim }, roleFromDb.Claims);
			}
			public async Task Returns_list_of_claims_from_role()
			{
				// arrange
				var role = new IdentityRole();
				var claim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "some value" };
				var claim2 = new IdentityClaim { ClaimType = "ClaimType2", ClaimValue = "some other value" };
				role.Claims.Add(claim1);
				role.Claims.Add(claim2);


				// act
				var result = await _roleStore.GetClaimsAsync(role);

				// assert
				Assert.Equal(role.Claims.Count, result.Count);
				Assert.True(result.Single(c => c.Type == claim1.ClaimType && c.Value == claim1.ClaimValue) != null);
				Assert.True(result.Single(c => c.Type == claim2.ClaimType && c.Value == claim2.ClaimValue) != null);

			}
			public async Task Remove_existing_claim_different_casing_updates_database_role_record()
			{
				// arrange
				var claim1 = new Claim("ClaimType", "some value");
				var claim2 = new Claim("ClaimType2", "some other value");
				var identityClaim1 = new IdentityClaim { ClaimType = claim1.Type.ToUpper(), ClaimValue = claim1.Value.ToUpper() };
				var identityClaim2 = new IdentityClaim(claim2);

				var role = new IdentityRole("Remove_existing_claim_updates_database_role_record");
				role.Claims.Add(identityClaim1);
				role.Claims.Add(identityClaim2);
				await _roleStore.CreateAsync(role);

				// act
				await _roleStore.RemoveClaimAsync(role, claim1);

				// assert

				// check role claims from memory
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, role.Claims);

				// check role claims from DB
				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, roleFromDb.Claims);
			}
			public void When_claims_dont_match_returns_false()
			{
				// arrange
				var c = new IdentityClaim { ClaimType = _identityClaim1SameType.ClaimType, ClaimValue = _identityClaim1SameType.ClaimValue }; ;


				// assert
				Assert.False(_identityClaim1.Equals(c));
			}
			public async Task When_User_has_Role_with_claims_and_claim_removed_from_Role_should_remove_cliam_from_User_Role_as_well()
			{
				// arrange
				var identityClaim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "val" };
				var identityClaim2 = new IdentityClaim { ClaimType = "ClaimType2", ClaimValue = "some val" };
				var role1 = new IdentityRole { Name = "Role1", Claims = new List<IdentityClaim> { identityClaim1, identityClaim2 } };
				var role2 = new IdentityRole { Name = "Role2", Claims = new List<IdentityClaim> { identityClaim1 } };

				await _roleStore.CreateAsync(role1);
				await _roleStore.CreateAsync(role2);

				var user1 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role1, role2 } };
				var user2 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role1 } };
				var user3 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role2 } };

				await _userStore.CreateAsync(user1);
				await _userStore.CreateAsync(user2);
				await _userStore.CreateAsync(user3);

				// act
				await _roleStore.RemoveClaimAsync(role1, new Claim(identityClaim1.ClaimType, identityClaim1.ClaimValue));

				// assert
				var user1FromDb = await _userCollection.Find(x => x.Id == user1.Id).SingleOrDefaultAsync();
				var user2FromDb = await _userCollection.Find(x => x.Id == user2.Id).SingleOrDefaultAsync();
				var user3FromDb = await _userCollection.Find(x => x.Id == user3.Id).SingleOrDefaultAsync();

				// USER 1
				Assert.Equal(user1.Roles.Count, user1FromDb.Roles.Count);
				var user1FromDbRole1 = user1FromDb.Roles.Single(x => x.Id == role1.Id);
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, user1FromDbRole1.Claims);

				// we were specifically removing the identityClaim1 from role1 - so role2 should still have identityClaim1 in role2
				var user1FromDbRole2 = user1FromDb.Roles.Single(x => x.Id == role2.Id);
				IdentityClaimAssert.Equal(role2.Claims, user1FromDbRole2.Claims);


				// USER 2
				Assert.Equal(user2.Roles.Count, user2FromDb.Roles.Count);
				var user2FromDbRole1 = user2FromDb.Roles.Single();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, user2FromDbRole1.Claims);


				// USER 3
				Assert.Equal(user3.Roles.Count, user3FromDb.Roles.Count);
				// we were specifically removing the identityClaim1 from role1 - so role2 should still have identityClaim1 in role2
				var user3FromDbRole2 = user3FromDb.Roles.Single();
				IdentityClaimAssert.Equal(role2.Claims, user3FromDbRole2.Claims);
			}
			public void When_claims_match_returns_true()
			{
				// arrange
				var c = new IdentityClaim { ClaimType = _identityClaim1.ClaimType, ClaimValue = _identityClaim1.ClaimValue }; ;


				// assert
				Assert.True(_identityClaim1.Equals(c));
			}
			public async Task When_User_has_Role_and_Role_is_deleted_should_remove_User_Role_as_well()
			{
				// arrange
				var identityClaim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "val" };
				var role1 = new IdentityRole { Name = "Role1", Claims = new List<IdentityClaim> { identityClaim1 } };
				var role2 = new IdentityRole { Name = "Role2", Claims = new List<IdentityClaim> { identityClaim1 } };

				await _roleStore.CreateAsync(role1);
				await _roleStore.CreateAsync(role2);

				var user1 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role1, role2 } };
				var user2 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role1 } };
				var user3 = new IdentityUser { UserName = "******", Roles = new List<IdentityRole<string>> { role2 } };

				await _userStore.CreateAsync(user1);
				await _userStore.CreateAsync(user2);
				await _userStore.CreateAsync(user3);

				// act
				await _roleStore.DeleteAsync(role1);

				// assert
				var user1FromDb = await _userCollection.Find(x => x.Id == user1.Id).SingleOrDefaultAsync();
				var user2FromDb = await _userCollection.Find(x => x.Id == user2.Id).SingleOrDefaultAsync();
				var user3FromDb = await _userCollection.Find(x => x.Id == user3.Id).SingleOrDefaultAsync();

				// USER 1
				Assert.Equal(1, user1FromDb.Roles.Count);
				var user1FromDbRole2 = user1FromDb.Roles.Single(x => x.Id == role2.Id);
				IdentityRoleAssert.Equal(role2, (IdentityRole)user1FromDbRole2);


				// USER 2
				Assert.Equal(0, user2FromDb.Roles.Count);


				// USER 3
				Assert.Equal(1, user3FromDb.Roles.Count);
				var user3FromDbRole2 = user3FromDb.Roles.Single();
				IdentityRoleAssert.Equal(role2, (IdentityRole)user3FromDbRole2);
			}
			public async Task Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value()
			{
				// arrange
				var claim1 = new Claim("ClaimType", "some value");
				var claim2 = new Claim(claim1.Type, "some other value");
				var identityClaim1 = new IdentityClaim(claim1);
				var identityClaim2 = new IdentityClaim(claim2);

				var role = new IdentityRole("Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value");
				role.Claims.Add(identityClaim1);
				role.Claims.Add(identityClaim2);
				await _roleStore.CreateAsync(role);

				// act
				await _roleStore.RemoveClaimAsync(role, claim1);

				// assert

				// check role claims from memory
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, role.Claims);

				// check role claims from DB
				var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();
				IdentityClaimAssert.Equal(new List<IdentityClaim> { identityClaim2 }, roleFromDb.Claims);
			}
		public IdentityClaimTests()
		{
			_identityClaim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "some value" };
			_identityClaim1SameType = new IdentityClaim { ClaimType = _identityClaim1.ClaimType, ClaimValue = _identityClaim1.ClaimValue + " different"};
		}
			public async Task Returns_all_claims_from_users_claims_and_roles()
			{
				// arrange
				var user = new IdentityUser();
				var claim1 = new IdentityClaim { ClaimType = "ClaimType1", ClaimValue = "some value" };
				var claim2 = new IdentityClaim { ClaimType = "ClaimType2", ClaimValue = "some other value" };
				var claim3 = new IdentityClaim { ClaimType = "ClaimType3", ClaimValue = "yet another value" };
				user.Claims.Add(claim1);
				user.Claims.Add(claim2);

				var role = new IdentityRole();
				role.Claims.Add(claim1);
				role.Claims.Add(claim3);
				user.Roles.Add(role);
				
				// act
				var result = await _userStore.GetClaimsAsync(user);

				// assert
				Assert.Equal(3, result.Count);
				Assert.True(result.Single(c => c.Type == claim1.ClaimType && c.Value == claim1.ClaimValue) != null);
				Assert.True(result.Single(c => c.Type == claim2.ClaimType && c.Value == claim2.ClaimValue) != null);
				Assert.True(result.Single(c => c.Type == claim3.ClaimType && c.Value == claim3.ClaimValue) != null);

			}