Exemple #1
0
        //Search Contact
        public List <DirectoryEntry> GetOUContactObjects(string FQDomainName, string DestinationOUDN, IOnPremADHelper OnPremADHelper, Logger Logger)
        {
            SearchResultCollection ListADContactResults;
            List <DirectoryEntry>  ADContacts = new List <DirectoryEntry>();

            //Will enter Directory at $ContactsDestinationOUDN and retrieve ALL properties (empty string array)
            using (DirectorySearcher ListADContacts = new DirectorySearcher(OnPremADHelper.GetADDirectoryEntry(FQDomainName, DestinationOUDN, Logger),
                                                                            "(objectClass=contact)", new string[] {}))
            {
                ListADContacts.PropertiesToLoad.Add("mail");
                ListADContactResults = ListADContacts.FindAll();
            }

            if (ListADContactResults != null)
            {
                //Get the corrosponding Directory Entry for each SearchResult object.
                foreach (SearchResult DirectoryEntry in ListADContactResults)
                {
                    ADContacts.Add(DirectoryEntry.GetDirectoryEntry());
                }

                return(ADContacts);
            }
            else
            {
                Logger.Debug(String.Format("No Contact objects found in {0}", DestinationOUDN));
                Console.WriteLine("No Contact objects found in {0}", DestinationOUDN);

                //Retrun an empty list.
                return(ADContacts);
            }
        }
Exemple #2
0
 public void AddADContactToGroup(Logger Logger, IConfiguration Config, IOnPremADHelper OnPremADHelper, string ContactDNPath)
 {
     try
     {
         DirectoryEntry GroupDirectoryEntry = OnPremADHelper.GetADDirectoryEntry(Config.FQDomainName, Config.PermittedSendersGroupDN, Logger);
         GroupDirectoryEntry.Properties["member"].Add(ContactDNPath);
         GroupDirectoryEntry.CommitChanges();
     }
     catch (Exception ex)
     {
         Logger.Error(string.Format("Error when adding Contact to Group {0} , {1} {2}", Config.PermittedSendersGroupDN, ex.Message, ex.InnerException != null ? ex.InnerException.Message : ""));
     }
 }
Exemple #3
0
        public void ContactSync(Logger Logger, IConfiguration config, IAuthenticationProvidor authProvidor, IAzureADFunctions azureAdFunctions, IOnPremADHelper onPremAdHelper, IOnPremAdFunctions onPremAdFunctions, ActiveDirectoryClient AzureClientSession)
        {
            //Get Entry into On-prem Active Directory Contacts OU.
            DirectoryEntry _OnPremContactsDirectoryEntry = onPremAdHelper.GetADDirectoryEntry(config.FQDomainName, config.ContactsDestinationOUDN, Logger);

            //Gather User Objects for the Work we intend to do later:
            Group _AzureUsersgroup = azureAdFunctions.GetADGroup(AzureClientSession, config.AzureADUserGroup, Logger);
            if (_AzureUsersgroup != null)
            {
                List<Tuple<string, IDirectoryObject>> _AzureGroupMembers = azureAdFunctions.GetAdGroupMembers(_AzureUsersgroup, config, Logger);

                if (_AzureGroupMembers.Any(members => members.Item1 == "user"))
                {
                    List<IUser> _AzureGroupUsers = _AzureGroupMembers.Where(member => member.Item1.Equals("user"))
                                                                     .Select(member => member.Item2)
                                                                     .Select(member => member as IUser)
                                                                     .ToList();

                    List<DirectoryEntry> _OnPremContactObjects = onPremAdFunctions.GetOUContactObjects(config.FQDomainName, config.ContactsDestinationOUDN, onPremAdHelper, Logger);

                    #region Add Contact Objects to AD Contacts OU
                    //foreach user in Cloud check if they reside onprem and add them if they dont.

                    if (config.AllowCreationOfADObjects)
                    {
                        Dictionary<string, IUser> azureUsers = _AzureGroupUsers.Where(x => x.Mail != null)
                                                                               .ToDictionary(x => x.Mail.ToLower(), x => x);

                        foreach (string OnPremUser in _OnPremContactObjects.Where(x => x.Properties["Mail"].Value != null)
                                                                           .Select(x => x.Properties["Mail"].Value.ToString()))
                        {
                            azureUsers.Remove(OnPremUser.ToLower());
                        }

                        int CreatedUsers = onPremAdFunctions.CreateADUserContacts(Logger, config, _OnPremContactsDirectoryEntry, onPremAdHelper, azureUsers);

                        Logger.Debug(String.Format("Created {0} user(s) in On-Prem Active Directory", CreatedUsers.ToString()));
                        Console.WriteLine("Created {0} user(s) in On-Prem Active Directory", CreatedUsers.ToString());

                    }
                    #endregion

                    #region Delete Contact Objects from AD OU
                    //foreach user onprem check if they reside in cloud - delete them from AD if they dont (Make this over-rideable with a key)
                    if (config.AllowDeletionOfADObjects)
                    {
                        Dictionary<string, DirectoryEntry> onpremUsers = _OnPremContactObjects.Where(y => y.Properties["Mail"].Value != null)
                                                                                              .ToDictionary(y => y.Properties["Mail"].Value.ToString().ToLower(), y => y);

                        foreach (string AzureUser in _AzureGroupUsers.Where(y => y.Mail != null)
                                                                     .Select(y => y.Mail.ToLower()))
                        {
                            onpremUsers.Remove(AzureUser.ToLower());
                        }

                        int DeletedUsers = onPremAdFunctions.DeleteADContacts(Logger, config, _OnPremContactsDirectoryEntry, onpremUsers);

                        Logger.Debug(String.Format("Deleted {0} user(s) in On-Prem Active Directory", DeletedUsers.ToString()));
                        Console.WriteLine("Deleted {0} user(s) in On-Prem Active Directory", DeletedUsers.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Could not find any USER objects in group {0}", config.AzureADUserGroup);
                    Logger.Error(String.Format("Could not find any USER objects in group {0}", config.AzureADUserGroup));
                }

            }
            else
            {
                Console.WriteLine("Could not find Group in Azure ({0} to enumerate users from", config.AzureADUserGroup);
                Logger.Error(String.Format("Could not find Group in Azure ({0} to enumerate users from", config.AzureADUserGroup));
            }

            //Close AD Directory Entry Handle
            onPremAdHelper.DisposeADDirectoryEntry(_OnPremContactsDirectoryEntry, Logger);

            Console.WriteLine("Contact Creation/Deletion complete - Changes will be reflected on Office365 Sync on Next Dir-Sync Cycle but may not appear in Address book until the following day.");
            Logger.Debug(@"Contact Creation/Deletion complete - Changes will be reflected on Office365 upon next DirSync.");

            #endregion
        }
Exemple #4
0
        public void GroupSync(Logger Logger, IConfiguration config, IAuthenticationProvidor authProvidor, IAzureADFunctions azureAdFunctions, IOnPremADHelper onPremAdHelper, IOnPremAdFunctions onPremAdFunctions, ActiveDirectoryClient AzureClientSession)
        {
            //Get Entry into On-prem Active Directory Groups OU.
            DirectoryEntry _OnPremGroupsDirectoryEntry = onPremAdHelper.GetADDirectoryEntry(config.FQDomainName, config.GroupsDestinationOUDN, Logger);

            //Gather User Objects for the Work we intend to do later:
            Group _AzureUsersgroup = azureAdFunctions.GetADGroup(AzureClientSession, config.AzureADGroupsGroup, Logger);

            if (_AzureUsersgroup != null)
            {
                List <Tuple <string, IDirectoryObject> > _AzureGroupMembers = azureAdFunctions.GetAdGroupMembers(_AzureUsersgroup, config, Logger);

                if (_AzureGroupMembers.Any(members => members.Item1 == "group"))
                {
                    List <IGroup> _AzureGroupGroups = _AzureGroupMembers.Where(member => member.Item1.Equals("group"))
                                                      .Select(member => member.Item2)
                                                      .Select(member => member as IGroup)
                                                      .ToList();

                    List <DirectoryEntry> _OnPremContactObjects = onPremAdFunctions.GetOUContactObjects(config.FQDomainName, config.GroupsDestinationOUDN, onPremAdHelper, Logger);

                    #region Add Contact Objects to AD Contacts OU
                    //foreach group in Cloud check if they reside onprem and add them if they dont.

                    if (config.AllowCreationOfADObjects)
                    {
                        Dictionary <string, IGroup> azureGroups = _AzureGroupGroups.Where(x => x.Mail != null)
                                                                  .ToDictionary(x => x.Mail.ToLower(), x => x);

                        foreach (string OnPremUser in _OnPremContactObjects.Where(x => x.Properties["Mail"].Value != null)
                                 .Select(x => x.Properties["Mail"].Value.ToString()))
                        {
                            azureGroups.Remove(OnPremUser.ToLower());
                        }

                        int CreatedUsers = onPremAdFunctions.CreateADGroupContacts(Logger, config, _OnPremGroupsDirectoryEntry, onPremAdHelper, azureGroups);

                        Logger.Debug(String.Format("Created {0} group(s) in On-Prem Active Directory", CreatedUsers.ToString()));
                        Console.WriteLine("Created {0} group(s) in On-Prem Active Directory", CreatedUsers.ToString());
                    }
                    #endregion

                    #region Delete Group Objects from AD OU

                    //foreach group onprem check if they reside in cloud - delete them from AD if they dont (Make this over-rideable with a key)
                    if (config.AllowDeletionOfADObjects)
                    {
                        Dictionary <string, DirectoryEntry> onpremGroups = _OnPremContactObjects.Where(y => y.Properties["Mail"].Value != null)
                                                                           .ToDictionary(y => y.Properties["Mail"].Value.ToString().ToLower(), y => y);

                        foreach (string AzureUser in _AzureGroupGroups.Where(y => y.Mail != null)
                                 .Select(y => y.Mail.ToLower()))
                        {
                            onpremGroups.Remove(AzureUser.ToLower());
                        }

                        int DeletedGroups = onPremAdFunctions.DeleteADContacts(Logger, config, _OnPremGroupsDirectoryEntry, onpremGroups);

                        Logger.Debug(String.Format("Deleted {0} group(s) in On-Prem Active Directory", DeletedGroups.ToString()));
                        Console.WriteLine("Deleted {0} group(s) in On-Prem Active Directory", DeletedGroups.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Could not find any GROUP objects in group {0}", config.AzureADUserGroup);
                    Logger.Error(String.Format("Could not find any GROUP objects in group {0}", config.AzureADUserGroup));
                }
            }
            else
            {
                Console.WriteLine("Could not find Group in Azure ({0} to enumerate users from", config.AzureADUserGroup);
                Logger.Error(String.Format("Could not find Group in Azure ({0} to enumerate users from", config.AzureADUserGroup));
            }

            //Close AD Directory Entry Handle
            onPremAdHelper.DisposeADDirectoryEntry(_OnPremGroupsDirectoryEntry, Logger);

            Console.WriteLine("Group Creation/Deletion complete - Changes will be reflected on Office365 Sync on Next Dir-Sync Cycle but may not appear in Address book until the following day.");
            Logger.Debug(@"Group Creation/Deletion complete - Changes will be reflected on Office365 upon next DirSync.");

            #endregion
        }