public ADCST(string arg, Logger Logger, IConfiguration config, IAuthenticationProvidor authProvidor, IAzureADFunctions azureAdFunctions, IOnPremADHelper onPremAdHelper, IOnPremAdFunctions onPremAdFunctions) { if (string.IsNullOrEmpty(arg)) { StartSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, false); } else { switch (arg.ToLower()) { case @"/h": case @"--h": case @"-h": case @"h": ShowHelp(); break; case @"/d": case @"--d": case @"-d": case @"d": StartSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, true); break; default: StartSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, false); break; } } }
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 : "")); } }
private void StartSync(Logger Logger, IConfiguration config, IAuthenticationProvidor authProvidor, IAzureADFunctions azureAdFunctions, IOnPremADHelper onPremAdHelper, IOnPremAdFunctions onPremAdFunctions, bool ShowDiagnostics) { ActiveDirectoryClient ClientSession = azureAdFunctions.ADClient(config, authProvidor, Logger); //Show Azure Tennant Diagnostics if requested. if(ShowDiagnostics) { Console.WriteLine(azureAdFunctions.TenantDetails(ClientSession, Logger, config)); } //TODO RE-ENABLE THE BELOW METHOD! //We're done outputting debug info - Call the applications main logic. _objContactManagement.ContactSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, ClientSession); _objGroupManagement.GroupSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, ClientSession); }
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 }
//Create User Contact objects public int CreateADUserContacts(Logger Logger, IConfiguration Config, DirectoryEntry DirEntry, IOnPremADHelper OnPremADHelper, Dictionary <string, IUser> AzureUsers) { int CreationCount = 0; Logger.Debug("Begining User Contact Creation..."); foreach (User AzureUser in AzureUsers.Values) { try { if (AzureUser.AccountEnabled.HasValue && AzureUser.AccountEnabled.Value) { DirectoryEntry newUser = DirEntry.Children.Add("CN=" + AzureUser.DisplayName, "contact"); //Add Each Property we care about for the respective User account (if not null): if (!string.IsNullOrEmpty(Config.ObjectPrefix)) { newUser.Properties["description"].Value = Config.ObjectPrefix; } if (!string.IsNullOrEmpty(AzureUser.GivenName)) { newUser.Properties["givenName"].Value = AzureUser.GivenName; } if (!string.IsNullOrEmpty(AzureUser.Mail)) { newUser.Properties["Mail"].Value = AzureUser.Mail; newUser.Properties["proxyAddresses"].AddRange(new object[] { "SMTP:" + AzureUser.Mail, "SIP:" + AzureUser.Mail }); } if (!string.IsNullOrEmpty(AzureUser.Surname)) { newUser.Properties["sn"].Value = AzureUser.Surname; } if (!string.IsNullOrEmpty(AzureUser.DisplayName)) { newUser.Properties["displayName"].Value = AzureUser.DisplayName; } if (!string.IsNullOrEmpty(AzureUser.JobTitle)) { newUser.Properties["title"].Value = AzureUser.JobTitle; } if (!string.IsNullOrEmpty(AzureUser.City)) { newUser.Properties["l"].Value = AzureUser.City; } if (!string.IsNullOrEmpty(AzureUser.Country)) { newUser.Properties["co"].Value = AzureUser.Country; newUser.Properties["c"].Value = AzureUser.UsageLocation; } if (!string.IsNullOrEmpty(AzureUser.State)) { newUser.Properties["st"].Value = AzureUser.State; } if (!string.IsNullOrEmpty(AzureUser.StreetAddress)) { newUser.Properties["streetAddress"].Value = AzureUser.StreetAddress; } if (!string.IsNullOrEmpty(AzureUser.Department)) { newUser.Properties["department"].Value = AzureUser.Department; } newUser.CommitChanges(); //Call Add Contact to Group if Key exists if (!string.IsNullOrEmpty(Config.PermittedSendersGroupDN)) { string UserDN = newUser.Path.Substring(newUser.Path.LastIndexOf('/') + 1); AddADContactToGroup(Logger, Config, OnPremADHelper, UserDN); } CreationCount++; if (Config.VerboseLogUserCreation) { Logger.Debug(String.Format("Created User Contact {0}", AzureUser.Mail)); } } } catch (Exception ex) { Logger.Error(String.Format("Error when Creating user contact {0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : "")); } } return(CreationCount); }
//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); } }
//Create Group Contact objects public int CreateADGroupContacts(Logger Logger, IConfiguration Config, DirectoryEntry DirEntry, IOnPremADHelper OnPremADHelper, Dictionary <string, IGroup> AzureGroups) { int CreationCount = 0; Logger.Debug("Begining Group Contact Creation..."); foreach (Group AzureGroup in AzureGroups.Values) { try { if (AzureGroup.MailEnabled == true) { DirectoryEntry newGroup = DirEntry.Children.Add("CN=" + AzureGroup.DisplayName, "contact"); //Add Each Property we care about for the respective User account (if not null): if (!string.IsNullOrEmpty(Config.ObjectPrefix)) { if (!string.IsNullOrEmpty(AzureGroup.Description)) { newGroup.Properties["description"].Value = String.Format("{0} - {1}", Config.ObjectPrefix, AzureGroup.Description); } else { newGroup.Properties["description"].Value = Config.ObjectPrefix; } } if (!string.IsNullOrEmpty(AzureGroup.Mail)) { newGroup.Properties["Mail"].Value = AzureGroup.Mail; newGroup.Properties["proxyAddresses"].AddRange(new object[] { "SMTP:" + AzureGroup.Mail }); //todo: ADD Any extra Proxy Addresses (if they exist as smtp:<somemail> } if (!string.IsNullOrEmpty(AzureGroup.DisplayName)) { newGroup.Properties["displayName"].Value = AzureGroup.DisplayName; //newGroup.Properties["name"].Value = AzureGroup.DisplayName; } newGroup.CommitChanges(); CreationCount++; if (Config.VerboseLogUserCreation) { Logger.Debug(String.Format("Created Group Contact {0}", AzureGroup.Mail)); } } } catch (Exception ex) { Logger.Error(String.Format("Error when Creating user contact {0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : "")); } } return(CreationCount); }
private void StartSync(Logger Logger, IConfiguration config, IAuthenticationProvidor authProvidor, IAzureADFunctions azureAdFunctions, IOnPremADHelper onPremAdHelper, IOnPremAdFunctions onPremAdFunctions, bool ShowDiagnostics) { ActiveDirectoryClient ClientSession = azureAdFunctions.ADClient(config, authProvidor, Logger); //Show Azure Tennant Diagnostics if requested. if (ShowDiagnostics) { Console.WriteLine(azureAdFunctions.TenantDetails(ClientSession, Logger, config)); } //TODO RE-ENABLE THE BELOW METHOD! //We're done outputting debug info - Call the applications main logic. _objContactManagement.ContactSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, ClientSession); _objGroupManagement.GroupSync(Logger, config, authProvidor, azureAdFunctions, onPremAdHelper, onPremAdFunctions, ClientSession); }
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 }