コード例 #1
0
        private static async Task SyncUserWithRolesAsync(AuthorizationCodeReceivedContext opt)
        {
            string          accessToken     = opt.ProtocolMessage.AccessToken;
            IApiClient      repository      = opt.HttpContext.RequestServices.GetService <IApiClient>();
            LoggedUserModel loggedUserModel = await repository.PostAsync <LoggedUserModel>("Account/SyncUserProfile", null, null, accessToken);

            if (!string.IsNullOrEmpty(opt.Principal.SupplierNumber()))
            {
                IdentityConfigs.SetNotSubscription(opt.Principal, loggedUserModel.SubscirpeUrl);
                IdentityConfigs.SetIsSubscription(opt.Principal, loggedUserModel.IsSubscripe);
            }
            if (loggedUserModel.AssignedRoleLevelTypeModels.Count() > 0)
            {
                string firstRole = "";
                var    nonRegisteredGovService = !loggedUserModel.IsSubscripe && opt.Principal.IsInRole(RoleNames.supplier) && loggedUserModel.AssignedRoleLevelTypeModels.Count > 1 && ((loggedUserModel.DefaultRoleDetails != null && loggedUserModel.DefaultRoleDetails.Contains(RoleNames.supplier)) || loggedUserModel.AssignedRoleLevelTypeModels.Where(x => x.AssignedRoleLevel == (int)Enums.AssignedRoleLevelType.NotAssigned).Select(s => s.GetDefaultRole).FirstOrDefault().Contains(RoleNames.supplier));
                if (!string.IsNullOrEmpty(loggedUserModel.DefaultRoleDetails) && loggedUserModel.AssignedRoleLevelTypeModels.Select(s => s.GetDefaultRole).Contains(loggedUserModel.DefaultRoleDetails) && !nonRegisteredGovService)
                {
                    firstRole = loggedUserModel.DefaultRoleDetails;
                }
                else if (loggedUserModel.AssignedRoleLevelTypeModels.Where(x => x.AssignedRoleLevel == (int)Enums.AssignedRoleLevelType.NotAssigned).Count() > 0)
                {
                    if (nonRegisteredGovService && loggedUserModel.AssignedRoleLevelTypeModels.Where(x => x.AssignedRoleLevel == (int)Enums.AssignedRoleLevelType.NotAssigned).Select(s => s.GetDefaultRole).FirstOrDefault().Contains(RoleNames.supplier))
                    {
                        firstRole = loggedUserModel.AssignedRoleLevelTypeModels.FirstOrDefault(a => !a.GetDefaultRole.Contains(RoleNames.supplier)).GetDefaultRole;
                    }
                    else
                    {
                        firstRole = loggedUserModel.AssignedRoleLevelTypeModels.Where(x => x.AssignedRoleLevel == (int)Enums.AssignedRoleLevelType.NotAssigned).Select(s => s.GetDefaultRole).FirstOrDefault();
                    }
                }
                else
                {
                    if (nonRegisteredGovService && loggedUserModel.AssignedRoleLevelTypeModels.FirstOrDefault().GetDefaultRole.Contains(RoleNames.supplier))
                    {
                        firstRole = loggedUserModel.AssignedRoleLevelTypeModels.FirstOrDefault(a => !a.GetDefaultRole.Contains(RoleNames.supplier)).GetDefaultRole;
                    }
                    else
                    {
                        firstRole = loggedUserModel.AssignedRoleLevelTypeModels.FirstOrDefault().GetDefaultRole;
                    }
                }
                string isBranchOrCommittee = firstRole.Split(',')[0]; // 1 =branch , 2 = Committee
                string branchOrCommitteeId = firstRole.Split(',')[1];
                string roleName            = firstRole.Split(',')[2];
                if (isBranchOrCommittee == ((int)Enums.AssignedRoleLevelType.Branch).ToString())
                {
                    IdentityConfigs.AddUserBranchId(opt.Principal, branchOrCommitteeId);
                }
                else if (isBranchOrCommittee == ((int)Enums.AssignedRoleLevelType.Committee).ToString())
                {
                    IdentityConfigs.AddUserCommittee(opt.Principal, branchOrCommitteeId);
                }
                IdentityConfigs.UpdateUserRoles(opt.Principal, new List <string> {
                    roleName
                });
            }
            else
            {
                IdentityConfigs.ReomveAllUserRoles(opt.Principal);
            }
            // Remove UnUsed Calims
            // 1
            IdentityConfigs.RemoveClaimByName(opt.Principal, "nationalityCode");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "englishFullname");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "englishFirstName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "englishSecondName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "englishThirdName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "englishLastName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "dateOfBirth");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "dateOfBirthHijri");
            // 2
            IdentityConfigs.RemoveClaimByName(opt.Principal, "permission");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "lastLoginGate");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "idExpiryDateStringHijri");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "idExpiryDateString");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "firstName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "secondName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "thirdName");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "lastName");

            IdentityConfigs.RemoveClaimByName(opt.Principal, "genderString");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "at_hash");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "c_hash");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "s_hash");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "nbf");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "aud");
            IdentityConfigs.RemoveClaimByName(opt.Principal, "name");
            IdentityConfigs.UpdateUserRolesList(opt.Principal, loggedUserModel.AssignedRoleLevelTypeModels);
        }