public async Task <Result <bool> > CreateClaimsPrincipal(ClaimsPrincipal claimsPrincipal) { var scopes = claimsPrincipal.Scopes; var claims = claimsPrincipal.Claims; scopes.ForEach(async x => await _userScopeRepository.CreateScope(new DALUserScopeModel { Type = x.Type, UserAccountId = claimsPrincipal.UserAccountId })); claims.ForEach(async x => await _userClaimRepository.CreateUserClaim(new DALUserClaimModel() { Type = x.Type, Value = x.Value, UserAccountId = claimsPrincipal.UserAccountId })); var allDalScopes = await _scopeRepository.GetAllScopes(); var allDalClaims = await _claimRepository.GetAllClaims(); var dalScopes = new List <ScopeModel>(); foreach (var dalScope in allDalScopes) { foreach (var scope in scopes) { if (dalScope.Type.ToUpper() == scope.Type.ToUpper()) { dalScopes.Add(dalScope); } } } var dalClaims = new List <ClaimModel>(); foreach (var dalClaim in allDalClaims) { foreach (var claim in claims) { if (dalClaim.Type.ToUpper() == claim.Type.ToUpper()) { dalClaims.Add(dalClaim); } } } var dalUserScopes = await _userScopeRepository.GetAllUserScopesByUserAccountId(claimsPrincipal.UserAccountId); var dalUserClaims = await _userClaimRepository.GetAllUserClaimsByUserAccountId(claimsPrincipal.UserAccountId); var allBlScopes = await _scopeService.GetAllScopes(); var allBlClaims = await _claimService.GetAllClaims(); var blScopes = new List <BusinessModels.UserAccessControl.ScopeModel>(); foreach (var blScope in allBlScopes) { foreach (var dalScope in dalUserScopes) { if (blScope.Type.ToUpper() == dalScope.Type.ToUpper()) { blScopes.Add(blScope); } } } var blClaims = new List <BusinessModels.UserAccessControl.ClaimModel>(); foreach (var blClaim in allBlClaims) { foreach (var dalClaim in dalClaims) { if (blClaim.Type.ToUpper() == dalClaim.Type.ToUpper()) { blClaims.Add(blClaim); } } } foreach (var dalUserScope in dalUserScopes) { var requiredClaims = blScopes.Where(x => x.Type == dalUserScope.Type).FirstOrDefault().Claims; var requiredDALClaims = dalUserClaims.Where(x => x.Type == (requiredClaims.Where(y => y.Type == x.Type).FirstOrDefault().Type)).ToList(); foreach (var dalUserClaim in dalUserClaims) { foreach (var requiredClaim in requiredClaims) { if (requiredClaim.Type.ToUpper() == dalUserClaim.Type.ToUpper()) { requiredDALClaims.Add(dalUserClaim); } } } requiredDALClaims.ForEach(async x => await _userScopeClaimRepository.CreateUserScopeClaim(new UserScopeClaimModel { UserAccountId = claimsPrincipal.UserAccountId, UserScopeId = dalUserScope.Id, UserClaimId = x.Id, Role = claimsPrincipal.Role })); } return(Result <bool> .Success(true)); }