private void RunSearchUser(string ldap, string netbios, int maxResultSet, string searchval) { try { DirectorySearcher searcher = new DirectorySearcher(GetDirectoryEntry(ldap)); searcher.Filter = string.Format("(&(objectcategory=person)(objectclass=user)(|(sAMAccountName={0}*)(displayName={0}*)(mail={0}*)))", searchval); if (maxResultSet == 0) { searcher.SizeLimit = base.ADMaxResultSize; } else { searcher.SizeLimit = maxResultSet; } searcher.PropertiesToLoad.Add(AdProperties.SamlAccountName); searcher.PropertiesToLoad.Add(AdProperties.DisplayName); searcher.PropertiesToLoad.Add(AdProperties.Email); foreach (string prop in AdditionalADProps) { searcher.PropertiesToLoad.Add(prop); } SearchResultCollection col = searcher.FindAll(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; DataRow dr; string saml; foreach (SearchResult res in col) { dr = results.NewRow(); saml = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.SamlAccountName); dr[Constants.SOProperties.ActiveDirectory.UserFQN] = string.Concat(netbios, "\\", saml); dr[Constants.SOProperties.ActiveDirectory.SamAccountName] = saml; dr[Constants.SOProperties.ActiveDirectory.DisplayName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.DisplayName); dr[Constants.SOProperties.ActiveDirectory.Email] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Email); foreach (string prop in AdditionalADProps) { dr[prop] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, prop); } lock (base.ServiceBroker.ServicePackage.ResultTable) { results.Rows.Add(dr); } } } catch (Exception ex) { throw new ApplicationException(string.Format("Failed to query {0}", ldap), ex); } }
private void GetUserDetails() { string userfqn = base.GetStringProperty(Constants.SOProperties.ActiveDirectory.UserFQN, true); string samlaccountname = userfqn.Substring(userfqn.IndexOf('\\') + 1); string[] ldaps = base.LDAPPaths.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string ldap in ldaps) { DirectorySearcher searcher = new DirectorySearcher(GetDirectoryEntry(ldap)); searcher.Filter = string.Format("(&(objectcategory=person)(objectclass=user)(sAMAccountName={0}))", EscapeSearchFilter(samlaccountname)); searcher.PageSize = base.ADMaxResultSize; SearchResult res = searcher.FindOne(); if (res != null) { ServiceObject serviceObject = base.ServiceBroker.Service.ServiceObjects[0]; serviceObject.Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; DataRow dr = results.NewRow(); dr[Constants.SOProperties.ActiveDirectory.SamAccountName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.SamlAccountName); dr[Constants.SOProperties.ActiveDirectory.DisplayName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.DisplayName); dr[Constants.SOProperties.ActiveDirectory.CommonName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.CommonName); dr[Constants.SOProperties.ActiveDirectory.GivenName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.GivenName); dr[Constants.SOProperties.ActiveDirectory.Initials] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Initials); dr[Constants.SOProperties.ActiveDirectory.Surname] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Surname); dr[Constants.SOProperties.ActiveDirectory.Email] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Email); dr[Constants.SOProperties.ActiveDirectory.OrganisationalUnit] = GetOUFromDistinguishedName(GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.DistinguishedName)); foreach (string prop in AdditionalADProps) { dr[prop] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, prop); } results.Rows.Add(dr); break; // there can be only one as this is a read method. } } #endregion GetUserDetails }
private void RunUMGetGroups(string ldap, string net) { Dictionary <string, string> inputProperties = new Dictionary <string, string>() { { Constants.SOProperties.URM.FQN, GetStringProperty(Constants.SOProperties.URM.FQN) }, { Constants.SOProperties.URM.Name, GetStringProperty(Constants.SOProperties.URM.Name) }, { Constants.SOProperties.URM.Description, GetStringProperty(Constants.SOProperties.URM.Description) }, { Constants.SOProperties.URM.Email, GetStringProperty(Constants.SOProperties.URM.Email) } }; //Adding additional AD properties to inputProperties for filtration foreach (string prop in AdditionalADProps) { inputProperties.Add(prop, GetStringProperty(prop)); } string securityLabel = GetStringParameter(Constants.SOProperties.URM.Label, true); DirectorySearcher dSearcher = new DirectorySearcher(new DirectoryEntry(ldap)); if (string.IsNullOrEmpty(securityLabel)) { securityLabel = "K2"; } dSearcher.Filter = LdapHelper.GetLdapQueryString(inputProperties, ServiceBroker.Service.ServiceObjects[0].Methods[0].Filter, IdentityType.Group, ChangeContainsToStartWith); dSearcher.PageSize = ADMaxResultSize; dSearcher.PropertiesToLoad.Add(AdProperties.sAMAccountName); dSearcher.PropertiesToLoad.Add(AdProperties.Name); dSearcher.PropertiesToLoad.Add(AdProperties.Email); dSearcher.PropertiesToLoad.Add(AdProperties.Description); //Adding additional AD Properties to load foreach (string prop in AdditionalADProps) { dSearcher.PropertiesToLoad.Add(prop); } SearchResultCollection col = dSearcher.FindAll(); DataTable results = ServiceBroker.ServicePackage.ResultTable; foreach (SearchResult res in col) { DataRow dr = results.NewRow(); string saml = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.sAMAccountName); dr[Constants.SOProperties.URM.FQN] = string.Concat(securityLabel, ":", net, "\\", saml); dr[Constants.SOProperties.URM.Name] = string.Concat(net, "\\", saml); dr[Constants.SOProperties.URM.GroupName] = string.Concat(net, "\\", saml); dr[Constants.SOProperties.URM.Description] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Description); dr[Constants.SOProperties.URM.Email] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Email); dr[Constants.SOProperties.URM.Saml] = saml; foreach (string prop in AdditionalADProps) { dr[prop] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, prop); } lock (ServiceBroker.ServicePackage.ResultTable) { results.Rows.Add(dr); } } }
private void RunGetUsers(string ldap, string netbios, int maxResultSet) { try { { DirectorySearcher searcher = new DirectorySearcher(GetDirectoryEntry(ldap)); StringBuilder searchFilter = new StringBuilder(); searchFilter.Append("(&"); searchFilter.Append("(objectcategory=person)(objectclass=user)"); string displayName = base.GetStringProperty(Constants.SOProperties.ActiveDirectory.DisplayName, false); string email = base.GetStringProperty(Constants.SOProperties.ActiveDirectory.Email, false); string userfqn = base.GetStringProperty(Constants.SOProperties.ActiveDirectory.UserFQN, false); if (!string.IsNullOrEmpty(displayName)) { searchFilter.AppendFormat("({0}={1})", AdProperties.DisplayName, displayName); } if (!string.IsNullOrEmpty(email)) { searchFilter.AppendFormat("({0}={1})", AdProperties.Email, email); } if (!string.IsNullOrEmpty(userfqn)) { searchFilter.AppendFormat("({0}={1})", AdProperties.SamlAccountName, userfqn.Substring(userfqn.IndexOf('\\') + 1)); } searchFilter.Append(")"); searcher.Filter = searchFilter.ToString(); if (maxResultSet == 0) { searcher.SizeLimit = base.ADMaxResultSize; } else { searcher.SizeLimit = maxResultSet; } searcher.PropertiesToLoad.Add(AdProperties.SamlAccountName); searcher.PropertiesToLoad.Add(AdProperties.DisplayName); searcher.PropertiesToLoad.Add(AdProperties.Email); foreach (string prop in AdditionalADProps) { searcher.PropertiesToLoad.Add(prop); } DataRow dr; string saml; SearchResultCollection col = searcher.FindAll(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; foreach (SearchResult res in col) { dr = results.NewRow(); saml = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.SamlAccountName); dr[Constants.SOProperties.ActiveDirectory.UserFQN] = string.Concat(netbios, "\\", saml); dr[Constants.SOProperties.ActiveDirectory.SamAccountName] = saml; dr[Constants.SOProperties.ActiveDirectory.DisplayName] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.DisplayName); dr[Constants.SOProperties.ActiveDirectory.Email] = GetSingleStringPropertyCollectionValue(res.Properties, AdProperties.Email); foreach (string prop in AdditionalADProps) { dr[prop] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, prop); } lock (base.ServiceBroker.ServicePackage.ResultTable) { results.Rows.Add(dr); } } } } catch (Exception ex) { throw new ApplicationException(string.Format("Failed to query {0}", ldap), ex); } }
private void RunUMGetUsers(string ldap, string net) { try { // we need the below because GetBoolProperty() returns false if the property value is null (not specified) which is not correct in this case string isAccountDisabled = null; if (!String.IsNullOrEmpty(GetStringProperty(Constants.SOProperties.URM.AccountIsDisabled))) { isAccountDisabled = GetBoolProperty(Constants.SOProperties.URM.AccountIsDisabled).ToString(); } Dictionary <string, string> inputProperties = new Dictionary <string, string>() { { Constants.SOProperties.URM.FQN, GetStringProperty(Constants.SOProperties.URM.FQN) }, { Constants.SOProperties.URM.Name, GetStringProperty(Constants.SOProperties.URM.Name) }, { Constants.SOProperties.URM.Description, GetStringProperty(Constants.SOProperties.URM.Description) }, { Constants.SOProperties.URM.Email, GetStringProperty(Constants.SOProperties.URM.Email) }, { Constants.SOProperties.URM.DisplayName, GetStringProperty(Constants.SOProperties.URM.DisplayName) }, { Constants.SOProperties.URM.Saml, GetStringProperty(Constants.SOProperties.URM.Saml) }, { Constants.SOProperties.URM.AccountIsDisabled, isAccountDisabled } }; //Adding additional AD properties to inputProperties for filtration foreach (string prop in AdditionalADProps) { inputProperties.Add(prop, GetStringProperty(prop)); } string securityLabel = GetStringParameter(Constants.SOProperties.URM.Label, true); DirectorySearcher dSearcher = new DirectorySearcher(new DirectoryEntry(ldap)); if (string.IsNullOrEmpty(securityLabel)) { securityLabel = "K2"; } dSearcher.Filter = LdapHelper.GetLdapQueryString(inputProperties, ServiceBroker.Service.ServiceObjects[0].Methods[0].Filter, IdentityType.User, ChangeContainsToStartWith); dSearcher.SizeLimit = ADMaxResultSize; dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.Name); dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.Email); dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.Description); dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.sAMAccountName); dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.DisplayName); dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.ObjectSID); //the Account Is Enabled property is in the UserAccountControl dSearcher.PropertiesToLoad.Add(Constants.Properties.AdProperties.UserAccountControl); //Adding additional AD Properties to load foreach (string prop in AdditionalADProps) { dSearcher.PropertiesToLoad.Add(prop); } SearchResultCollection col = dSearcher.FindAll(); DataTable results = ServiceBroker.ServicePackage.ResultTable; foreach (SearchResult res in col) { DataRow dr = results.NewRow(); string saml = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.sAMAccountName); dr[Constants.SOProperties.URM.FQN] = string.Concat(securityLabel, ":", net, "\\", saml); dr[Constants.SOProperties.URM.Name] = string.Concat(net, "\\", saml); dr[Constants.SOProperties.URM.UserName] = string.Concat(net, "\\", saml); dr[Constants.SOProperties.URM.Description] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.Description); dr[Constants.SOProperties.URM.Email] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.Email); dr[Constants.SOProperties.URM.DisplayName] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.DisplayName); dr[Constants.SOProperties.URM.ObjectSid] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.ObjectSID); dr[Constants.SOProperties.URM.Manager] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.Manager); dr[Constants.SOProperties.URM.Saml] = saml; dr[Constants.SOProperties.URM.AccountIsDisabled] = Convert.ToBoolean( (int)Constants.UserAccountControl.PropertyFlags.ACCOUNTDISABLE & int.Parse(LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, Constants.Properties.AdProperties.UserAccountControl)) ); //Adding additional AD Properties foreach (string prop in AdditionalADProps) { dr[prop] = LdapHelper.GetSingleStringPropertyCollectionValue(res.Properties, prop); } lock (ServiceBroker.ServicePackage.ResultTable) { results.Rows.Add(dr); } } } catch (Exception ex) { throw new ApplicationException(string.Format(Resources.CaughtExceptionAtRunUMGetUsers, ldap, net), ex); } }