public void It_creates_and_inserts_new_global_claims() { var result = 0; List <IdentityClaim> newClaims; using (var transaction = _database.GetTransaction()) { result = CreateClaim(claimType1, claimValueAccount1); result = CreateClaim(claimType1, claimValueAccount2); newClaims = _claimTable.GetClaims() .Where(c => c.Type == claimType1) .ToList(); transaction.Dispose(); } Assert.IsNotNull(newClaims); Assert.AreEqual(2, newClaims.Count()); Assert.IsTrue(newClaims.FirstOrDefault().Id.StartsWith("CL")); Assert.IsNull(newClaims.FirstOrDefault().ClientId); Assert.AreEqual(claimType1, newClaims.FirstOrDefault().Type); Assert.AreEqual(claimValueAccount1, newClaims.FirstOrDefault().Value); }
// ----- User-Claim funcionality -------------------------------------- /// <summary> /// Adds a claim to the user /// Different approach here: check if the Claim object exists in the Claim table /// and if so: identify by getting its Id and create the claim assigment to the user /// </summary> /// <param name="user"></param> /// <param name="claim"></param> /// <returns></returns> public Task AddClaimAsync(TUser user, Claim claim) { if (user == null) { throw new ArgumentNullException(IdentityConstants.User); } var claimsFound = _claimTable.GetClaims() .Where(c => c.Type == claim.Type && c.Value == claim.Value).ToList(); if (!claimsFound.Any()) { throw new ArgumentNullException(IdentityConstants.Claim); } var specificClaim = claimsFound.FirstOrDefault(c => c.ClientId == user.ClientId); var globalClaim = claimsFound.FirstOrDefault(c => c.ClientId == null); var claimToAdd = specificClaim ?? globalClaim; _userClaimTable.Insert(claimToAdd.Id, user.Id); return(Task.FromResult <object>(null)); }