private static void uniTest() { //using (var fs = new System.IO.StreamWriter(Path.Combine(HomeFolder, "cpr.txt"), false)) //{ // fs.Write(cprContent); // fs.Close(); //} UserProperties userPropsFrom = new UserProperties() { { "L", new string[] { "Boston", "Tokio" } }, { "facsimileTelephoneNumber", null }, // new string[]{"123"} { "mobile", new string[] { "7" } }, { "memberOf", new string[] { "CN=Exchange Services,CN=Users,DC=egar,DC=egartech,DC=com", "CN=IIS_IUSRS,CN=Builtin,DC=kireev,DC=local" } }, { "mail", new string[] { "123DfG123" } }, { "userPrincipalName", new string[] { "123" } }, }; UserProperties userPropsTo = new UserProperties() { { "L", new string[] { "Boston" } }, { "facsimileTelephoneNumber", new string[] { "456" } }, { "userAccountControl", new string[] { Convert.ToString((uint)Utils.UserAccountControl.SMARTCARD_REQUIRED) } }, { "mobile", new string[] { "4" } }, { "memberOf", new string[] { "CN=Exchange Services,CN=Users,DC=egar,DC=egartech,DC=com", "CN=IIS_IUSRS,CN=Builtin,DC=kireev,DC=local" } }, }; string transResult; ADHintElement adHint = ADHintsConfigurationSection.GetOUByAttributes(userPropsTo, userPropsFrom, out transResult); if (adHint != null) { var qualityCheck = adHint.QualityCheck(userPropsFrom); //var transResult = adHint.GetTransitionByUserAttributes(userPropsFrom, userPropsTo); } }
private static void InitializeAllAccounts() { CacheAllGroups(); log.LogInfo("Initialize accounts ..."); PollAD adSource = GetAvailableAD(config.SourceADServers, lastHighUSNs, true); PollAD adDest = GetAvailableAD(config.DestADServers, lastHighUSNs, true); lastHighUSNs[adSource.GetInvocationID] = adSource.CurrentHighUSN; try { IDictionary <string, UserProperties> userByObjectSID = new Dictionary <string, UserProperties>(adDest.ChangedUsersProperties.Count); IDictionary <string, UserProperties> userBySamAccount = new Dictionary <string, UserProperties>(adDest.ChangedUsersProperties.Count); log.LogInfo("Loaded " + adSource.ChangedUsersProperties.Count + " accounts from SourceAD " + adSource.DnsHostName + " and " + adDest.ChangedUsersProperties.Count + " accounts from Destination AD " + adDest.DnsHostName); foreach (var userProps in adDest.ChangedUsersProperties) { userProps.Remove("ObjectSID"); if (userProps.ContainsKey("Pager") && userProps["Pager"] != null) { userByObjectSID[userProps["Pager"][0]] = userProps; } userBySamAccount[userProps["samAccountName"][0]] = userProps; } log.LogDebug(" " + userByObjectSID.Count + " users has initialized ObjectSID in DestAD"); var changedUsers = new List <UserProperties>(); var cnt = adSource.ChangedUsersProperties.Count; FilterAccounts(adSource.ChangedUsersProperties, config.DestADServers.Select(s => s.ServerUserName), oUsToMonitor, oUsDNToMonitor); if (cnt - adSource.ChangedUsersProperties.Count > 0) { log.LogInfo("Filtered out " + (cnt - adSource.ChangedUsersProperties.Count) + " accounts"); } // Compare Source and Destination users ... foreach (var userProps in adSource.ChangedUsersProperties) { UserProperties destUser = null; if (!userByObjectSID.TryGetValue(userProps["ObjectSID"][0], out destUser) || destUser == null) { userBySamAccount.TryGetValue(userProps["samAccountName"][0], out destUser); } if (destUser == null) // not found by SID nor samAccountName { log.LogDebug(" '" + userProps["samAccountName"][0] + "' is new user"); changedUsers.Add(userProps); // new user } else { ADHintElement adHint = null; try { string notUsed; adHint = ADHintsConfigurationSection.GetOUByAttributes(userProps, destUser, out notUsed); } catch (Exception) { } // {[distinguishedName, CN=user3. sdfsdf,OU=Office31,OU=Office3,OU=Domain Controllers,DC=kireev,DC=local]} // simple way to determine if OU is changed if (adHint != null && destUser["distinguishedName"][0].IndexOf(adHint.DestOU, StringComparison.OrdinalIgnoreCase) < 0) { log.LogDebug(" '" + destUser["distinguishedName"][0] + "' need to move to " + adHint.DestOU); changedUsers.Add(userProps); // OU is changed } else { foreach (var prop in userProps) { string[] destPropVal; if (!PollAD.propIgnoreDest.Contains(prop.Key) && destUser.TryGetValue(prop.Key, out destPropVal) && prop.Value != null && !Utils.CheckEquals(prop.Value, destPropVal)) { log.LogDebug(" '" + userProps["samAccountName"][0] + "' changed [" + prop.Key + "]='" + Utils.PropVal(destPropVal) + "' -> '" + Utils.PropVal(prop.Value) + "'"); changedUsers.Add(userProps); break; } } } } } if (changedUsers.Count == 0) { log.LogInfo("Initialization complete. No differences between SourceAD and DestinationAD found."); } else { log.LogInfo("Need to update " + changedUsers.Count + " accounts..."); var updatedCnt = PutToDestinationAD(config.DestADServers, changedUsers, true); log.LogInfo("Initialization complete. Successfully updated " + updatedCnt + " of " + changedUsers.Count + " accounts."); } } catch (Exception ex) { log.LogError(ex, "Failed to Initialize: " + ex.Message); return; } }