コード例 #1
0
        public void InsertUserClaim()
        {
            IdentityUser user = new IdentityUser {
                UserName = "******", Email = "*****@*****.**", PhoneNumber = "1234567890", PasswordHash = "@#$rfgt$##WE", EmailConfirmed = true, PhoneNumberConfirmed = true, TwoFactorEnabled = false, LockoutEnabled = false, AccessFailedCount = 0
            };

            user = userTable.Insert(user);
            object claim = new { UserId = user.Id, ClaimType = "Claim1", ClaimValue = "Value1" };

            claim = userClaimTable.Insert(claim);
            int count = userClaimTable.All("ClaimType= 'Claim1'").ToList().Count;

            Assert.AreEqual(1, count);
        }
コード例 #2
0
        // ----- 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));
        }
コード例 #3
0
 private static int CreateUserClaim(IdentityUser user, string type, string value)
 {
     return(_userClaimTable.Insert(new Claim(type, value), user.Id));
 }