} // End Function Groups

        // http://stackoverflow.com/questions/45437/determining-members-of-local-groups-via-c-sharp
        public static System.Collections.Generic.List <string> AttributeValuesMultiString(string attributeName, string objectDn
                                                                                          , System.Collections.Generic.List <string> valuesCollection, bool recursive)
        {
            using (System.DirectoryServices.DirectoryEntry ent = new System.DirectoryServices.DirectoryEntry(objectDn))
            {
                System.DirectoryServices.PropertyValueCollection ValueCollection = ent.Properties[attributeName];
                System.Collections.IEnumerator en = ValueCollection.GetEnumerator();

                while (en.MoveNext())
                {
                    if (en.Current != null)
                    {
                        if (!valuesCollection.Contains(en.Current.ToString()))
                        {
                            valuesCollection.Add(en.Current.ToString());
                            if (recursive)
                            {
                                AttributeValuesMultiString(attributeName, "LDAP://" + en.Current.ToString(), valuesCollection, true);
                            } // End if (recursive)
                        }     // End if (!valuesCollection.Contains(en.Current.ToString()))
                    }         // End if (en.Current != null)
                }             // Whend

                ent.Close();
                // ent.Dispose();
            } // End Using DirectoryEntry ent

            return(valuesCollection);
        } // End Function AttributeValuesMultiString
 private static void RefillCollection(string srcValue, System.DirectoryServices.PropertyValueCollection targetValues)
 {
     targetValues.Clear();
     if (string.IsNullOrWhiteSpace(srcValue) == false)
     {
         string[] values = srcValue.Split(';');
         for (int i = values.Length - 1; i >= 0; i--)
         {
             if (string.IsNullOrWhiteSpace(values[i]) == false)
             {
                 targetValues.Add(values[i]);
             }
         }
     }
 }
Esempio n. 3
0
 public static string PropertyValueCollection(PSObject instance)
 {
     if (instance == null)
     {
         return(string.Empty);
     }
     System.DirectoryServices.PropertyValueCollection baseObject = (System.DirectoryServices.PropertyValueCollection)instance.BaseObject;
     if (baseObject == null)
     {
         return(string.Empty);
     }
     if (baseObject.Count != 1)
     {
         return(PSObject.ToStringEnumerable(instance.Context, (IEnumerable)baseObject, (string)null, (string)null, (IFormatProvider)null));
     }
     return(baseObject[0] == null ? string.Empty : PSObject.AsPSObject(baseObject[0]).ToString());
 }
Esempio n. 4
0
        static void GetGroupMembers()
        {
            string ldapHost = MySamples.TestSettings.ldapHost;
            int    ldapPort = MySamples.TestSettings.ldapPort;//System.Convert.ToInt32(args[1]);

            string msldap = $"LDAP://{ldapHost}:{ldapPort}/DC=COR,DC=local";
            string ms1    = $"LDAP://{ldapHost}:{ldapPort}/OU=Gruppen,OU=COR,DC=COR,DC=local";

            string loginDN  = MySamples.TestSettings.loginDN;  // args[2];
            string password = MySamples.TestSettings.password; // args[3];

            string strGroup = "COR-VMPost";

            strGroup = "G-ADM-APERTURE-UAT";

            // System.DirectoryServices.AccountManagement.
            //bool valid = false;
            //// https://stackoverflow.com/questions/326818/how-to-validate-domain-credentials
            //using (System.DirectoryServices.AccountManagement.PrincipalContext context =
            //    new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
            //{
            //    valid = context.ValidateCredentials("username", "password");
            //}

            bool bException = false;

            using (System.DirectoryServices.DirectoryEntry ldapConnection =
                       new System.DirectoryServices.DirectoryEntry(msldap, loginDN, password))
            {
                try
                {
                    // deRootObject.boun
                    if (ldapConnection.NativeObject == null)
                    {
                        bException = true;
                    }
                }
                catch (System.Exception ex)
                {
                    bException = true;
                    System.Console.WriteLine(ex.Message);
                    System.Console.WriteLine(ex.StackTrace);
                    throw new System.InvalidOperationException("Cannot login with wrong credentials or LDAP-Path.");
                }

                using (System.DirectoryServices.DirectorySearcher dsSearcher =
                           new System.DirectoryServices.DirectorySearcher(ldapConnection))
                {
                    dsSearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
                    dsSearcher.Filter      = "(&(objectCategory=group)(CN=" + strGroup + "))";

                    using (System.DirectoryServices.SearchResultCollection srcSearchResultCollection =
                               dsSearcher.FindAll())
                    {
                        try
                        {
                            foreach (System.DirectoryServices.SearchResult srSearchResult in srcSearchResultCollection)
                            {
                                System.DirectoryServices.ResultPropertyCollection resultPropColl = srSearchResult.Properties;
                                System.DirectoryServices.PropertyValueCollection  memberProperty = srSearchResult.GetDirectoryEntry().Properties["member"];

                                for (int i = 0; i < memberProperty.Count; ++i)
                                {
                                    string strUserName = System.Convert.ToString(memberProperty[i]);
                                    System.Console.WriteLine(strUserName);
                                } // Next i
                            }     // Next srSearchResult
                        }         // End Try
                        catch (System.Exception ex)
                        {
                            System.Console.WriteLine(ex.Message);
                            System.Console.WriteLine(ex.StackTrace);
                        }
                    } // End using srcSearchResultCollection
                }     // End Using dsSearcher
            }         // End Using ldapConnection

            System.Console.WriteLine(System.Environment.NewLine);
            System.Console.WriteLine(" --- Press any key to continue --- ");
            System.Console.ReadKey();
        }