dduminda 0 Newbie Poster

I developed Application for active directory.In that application I can create and retrive data without any problem.If I removed or added (update) user group to user it changed in active directory, when retrieving data some time shows old data.This porblem occurred only retrieving data but it actualy changed in the active directory.Please help me

internal static void RemoveUserFromGroup(string strContainerName, string strADUsrname, string strUserGroupName)
         {
             DirectoryEntry de = new DirectoryEntry();
             de.Path = "LDAP://DC=lk,DC=kworld,DC=kpmg,DC=com";
             de.AuthenticationType = AuthenticationTypes.Secure;
             de.RefreshCache();
            //--------- Add Security Group  ------------------------------------------------------------------------------------------------------------------------------
             if (IsUserGroupMeber(strADUsrname, strUserGroupName.ToString().Substring(3, strUserGroupName.ToString().Length - 3)) == true)
             {
                 PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, GlobalClass.DomainName, GlobalClass.AdminUserID, GlobalClass.AdminPassword);
                 UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, strADUsrname);
                 GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, strUserGroupName.ToString().Substring(3, strUserGroupName.ToString().Length - 3));
                 if (oUserPrincipal != null && oGroupPrincipal != null)
                 {
                         oGroupPrincipal.Members.Remove(oUserPrincipal);
                         oGroupPrincipal.Save();


                 }

                 oUserPrincipal.Dispose();
                 oGroupPrincipal.Dispose();
             }
            //--------- End Add Security Group  --------------------------------------------------------------------------------------------------------------------------

             de.RefreshCache();
             de.Dispose();
             de.Close();
        }

        internal static bool IsUserGroupMeber(string strADUsrname, string strUserGroup)
        {
            DirectoryEntry de = new DirectoryEntry();
            de.Path = "LDAP://DC=lk,DC=kworld,DC=kpmg,DC=com";
            de.AuthenticationType = AuthenticationTypes.Secure;
            de.RefreshCache();

            bool IsexistsInGroup = false;
            PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, GlobalClass.DomainName, GlobalClass.AdminUserID, GlobalClass.AdminPassword);
            UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, strADUsrname);

            GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, strUserGroup);

            if (oUserPrincipal != null && oGroupPrincipal != null)
            {
                //var existsInGroup = oGroupPrincipal.GetMembers(true).Where(p => p.UserPrincipalName == strADUsrname +"@kpmg.com").Any();

                //return existsInGroup;

                IsexistsInGroup = oGroupPrincipal.GetMembers(true).Contains(oUserPrincipal);
                oUserPrincipal.Dispose();
                oGroupPrincipal.Dispose();
                de.RefreshCache();
                de.Dispose();
                de.Close();
                return IsexistsInGroup;
            }
            else
            {
                de.RefreshCache();
                de.Dispose();
                de.Close();
                return false;
            }
        }