Beispiel #1
0
        private List <string> GetParentDomainPaths(DirectoryEntry directoryEntry, List <string> parentPaths)
        {
            PropertyValueCollection memberValueCollection = directoryEntry.Properties[ActiveDirectoryProperties.MemberOf];

            if (memberValueCollection != null)
            {
                IEnumerator memberEnumerator = memberValueCollection.GetEnumerator();
                while (memberEnumerator.MoveNext())
                {
                    if (memberEnumerator.Current != null)
                    {
                        string currentValue = AdDomainPathHandler.Escape(memberEnumerator.Current.ToString());
                        if (!parentPaths.Contains(currentValue))
                        {
                            parentPaths.Add(currentValue);

                            string fullDomainPath = domainPath.GetPathWithProtocol() + "/" + currentValue;
                            using (var entry = new DirectoryEntry(fullDomainPath))
                            {
                                GetParentDomainPaths(entry, parentPaths);
                            }
                        }
                    }
                }
            }

            return(parentPaths);
        }
        private IEnumerable <ObjectValues> setPropertyValue(string LDAPPath, string propertyName, string propertyValue)
        {
            DirectoryEntry objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);

            objRootDSE.Properties[propertyName].Add(propertyValue);
            objRootDSE.CommitChanges();
            objRootDSE.Close();

            objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
            //Multi-Value Property
            if (objRootDSE.Properties[propertyName].Count > 1)
            {
                PropertyValueCollection ValueCollection = objRootDSE.Properties[propertyName];
                IEnumerator             en = ValueCollection.GetEnumerator();

                while (en.MoveNext())
                {
                    if (en.Current != null)
                    {
                        yield return(new ObjectValues(LDAPPath, propertyName, en.Current.ToString()));
                    }
                }
            }
            //Single Value Property
            else
            {
                yield return(new ObjectValues(LDAPPath, propertyName, objRootDSE.Properties[propertyName].Value.ToString()));
            }
        }
Beispiel #3
0
        internal static string RetriveWkDn(DirectoryEntry deBase, string defaultNamingContext, string serverName, byte[] wellKnownContainerGuid)
        {
            string str;
            PropertyValueCollection item       = deBase.Properties["wellKnownObjects"];
            IEnumerator             enumerator = item.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    UnsafeNativeMethods.IADsDNWithBinary current = (UnsafeNativeMethods.IADsDNWithBinary)enumerator.Current;
                    if (!Utils.AreBytesEqual(wellKnownContainerGuid, (byte[])current.BinaryValue))
                    {
                        continue;
                    }
                    str = string.Concat("LDAP://", serverName, "/", current.DNString);
                    return(str);
                }
                return(null);
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            return(str);
        }
Beispiel #4
0
        /// <summary>This method is used for debug to print the raw Active Directory Properties of a User</summary>
        /// <param name="strLoginID">The LoginID (samAccountName) of the User whose properties need to be returned</param>
        /// <returns>string</returns>
        public string GetRawUserProperties(string strLoginID)
        {
            try
            {
                StringBuilder sb = new StringBuilder("");
                System.DirectoryServices.PropertyCollection pc = this.GetUserByLoginID(strLoginID).Properties;
                IDictionaryEnumerator IEnumProps = pc.GetEnumerator();

                // Loop the Properties collection
                while (IEnumProps.MoveNext())
                {
                    // Print the Key
                    sb.Append(IEnumProps.Key.ToString());

                    // Loop the Values collection
                    PropertyValueCollection pvc         = (System.DirectoryServices.PropertyValueCollection)IEnumProps.Value;
                    IEnumerator             IEnumValues = pvc.GetEnumerator();
                    while (IEnumValues.MoveNext())
                    {
                        // Print the Value
                        sb.Append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + IEnumValues.Current.ToString());
                    }
                    sb.Append("<br>");
                }

                // Append strMessage with Property output
                return(sb.ToString());
            }
            catch
            {
                return("Error retrieving Login Properties");
            }
        }
        /// <summary>
        /// Attributes the values multi string.
        /// </summary>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="objectDn">The object dn.</param>
        /// <param name="valuesCollection">The values collection.</param>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns></returns>
        public static ArrayList AttributeValuesMultiString(string attributeName, string objectDn, ArrayList valuesCollection, bool recursive)
        {
            DirectoryEntry          ent             = new DirectoryEntry(objectDn);
            PropertyValueCollection ValueCollection = ent.Properties[attributeName];
            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);
                        }
                    }
                }
            }
            ent.Close();
            ent.Dispose();
            return(valuesCollection);
        }
Beispiel #6
0
        // returns null if no more results
        private IEnumerator GetNextChunk()
        {
            string rangedAttribute = String.Format(
                CultureInfo.InvariantCulture,
                "{0};range={1}-*",
                _propertyName,
                _lowRange);

            GlobalDebug.WriteLineIf(GlobalDebug.Info, "RangeRetriever", "GetNextChunk: rangedAttribute={0}", rangedAttribute);

            try
            {
                // Pull in the next chunk of results
                _de.RefreshCache(new string[] { rangedAttribute, _propertyName });
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                if (e.ErrorCode == unchecked ((int)0x80072020))
                {
                    // ran out of results to retrieve
                    GlobalDebug.WriteLineIf(GlobalDebug.Info, "RangeRetriever", "GetNextChunk: no more results");
                    return(null);
                }

                // unknown failure, don't want to suppress it
                GlobalDebug.WriteLineIf(GlobalDebug.Error, "RangeRetriever", "GetNextChunk: caught COMException, ErrorCode={0}", e.ErrorCode);

                throw;
            }

            PropertyValueCollection pvc = _de.Properties[_propertyName];

            if (pvc == null || pvc.Count == 0)
            {
                // No results (the property may have been empty)
                GlobalDebug.WriteLineIf(GlobalDebug.Info, "RangeRetriever", "GetNextChunk: empty property?");
                return(null);
            }
            else
            {
                _lowRange = _lowRange + pvc.Count;

                GlobalDebug.WriteLineIf(GlobalDebug.Info,
                                        "RangeRetriever",
                                        "GetNextChunk: new lowRange={0}",
                                        _lowRange);

                return(pvc.GetEnumerator());
            }
        }
Beispiel #7
0
        private IEnumerable <ObjectValues> removeObjectPropertyValue(string LDAPPath, string propertyName, string propertyValue)
        {
            DirectoryEntry objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);

            if (objRootDSE.Properties.Contains(propertyName))
            {
                if (objRootDSE.Properties[propertyName].Contains(propertyValue))
                {
                    objRootDSE.Properties[propertyName].Remove(propertyValue);
                    objRootDSE.CommitChanges();
                    objRootDSE.Close();

                    objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
                    if (objRootDSE.Properties.Contains(propertyName))
                    {
                        //Multi-Value Property
                        if (objRootDSE.Properties[propertyName].Count > 1)
                        {
                            PropertyValueCollection ValueCollection = objRootDSE.Properties[propertyName];
                            IEnumerator             en = ValueCollection.GetEnumerator();

                            while (en.MoveNext())
                            {
                                if (en.Current != null)
                                {
                                    yield return(new ObjectValues(LDAPPath, propertyName, en.Current.ToString()));
                                }
                            }
                        }
                        //Single Value Property
                        else
                        {
                            yield return(new ObjectValues(LDAPPath, propertyName, objRootDSE.Properties[propertyName].Value.ToString()));
                        }
                    }
                    else
                    {
                        yield return(new ObjectValues(LDAPPath, propertyName, "NULL"));
                    }
                }
                else
                {
                    throw new Exception("Current Object " + LDAPPath + " does not contain any instance of Property " + propertyName + " with value " + propertyValue);
                }
            }
            else
            {
                throw new Exception("Current Object " + LDAPPath + " does not contain any instance of Property " + propertyName);
            }
        }
Beispiel #8
0
        public static String FindIIS()
        {
            String result = null;

            DirectoryEntry entry = null;

            try {
                entry = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");

                if (entry.Properties != null)
                {
                    Object val = entry.Properties["Path"];
                    if (val != null && (val is PropertyValueCollection))
                    {
                        PropertyValueCollection collection = (PropertyValueCollection)val;
                        IEnumerator             enumerator = collection.GetEnumerator();

                        if (enumerator.MoveNext())
                        {
                            result = (String)enumerator.Current;
                        }
                    }
                }
            }
            catch (Exception e) {
                log.WriteEntry(e.Message + "\n" + e.StackTrace);
            }
            finally {
                if (entry != null)
                {
                    entry.Close();
                }
            }

            if (result != null)
            {
                int i = result.LastIndexOf('\\');
                if (i > -1)
                {
                    result = result.Substring(0, i) + @"\Scripts";
                    if (!Directory.Exists(result))
                    {
                        Directory.CreateDirectory(result);
                    }
                }
            }

            return(result);
        }
Beispiel #9
0
        private IEnumerator GetNextChunk()
        {
            IEnumerator enumerator;

            object[] objArray = new object[2];
            objArray[0] = this.propertyName;
            objArray[1] = this.lowRange;
            string str = string.Format(CultureInfo.InvariantCulture, "{0};range={1}-*", objArray);

            try
            {
                string[] strArrays = new string[2];
                strArrays[0] = str;
                strArrays[1] = this.propertyName;
                this.de.RefreshCache(strArrays);
                goto Label0;
            }
            catch (COMException cOMException1)
            {
                COMException cOMException = cOMException1;
                if (cOMException.ErrorCode != -2147016672)
                {
                    throw;
                }
                else
                {
                    enumerator = null;
                }
            }
            return(enumerator);

Label0:
            PropertyValueCollection item = this.de.Properties[this.propertyName];

            if (item == null || item.Count == 0)
            {
                return(null);
            }
            else
            {
                this.lowRange = this.lowRange + item.Count;
                return(item.GetEnumerator());
            }
        }
Beispiel #10
0
        public static bool CheckComputerSecurityGroup(string computerName, string groupNames, string domain)
        {
            List <string> groupName = groupNames.ToUpper().Split(',').ToList();
            List <string> groups    = new List <string>();

            PrincipalContext  domainContext = new PrincipalContext(ContextType.Domain);
            ComputerPrincipal computer      = ComputerPrincipal.FindByIdentity(domainContext, computerName);

            using (var directoryEntry = new DirectoryEntry("LDAP://" + computer.DistinguishedName))
            {
                PropertyValueCollection ValueCollection = directoryEntry.Properties["memberOf"];
                IEnumerator             en = ValueCollection.GetEnumerator();
                while (en.MoveNext())
                {
                    if (en.Current != null)
                    {
                        string group = en.Current.ToString().Substring(3, (en.Current.ToString()).IndexOf(',') - 3);
                        if (!groups.Contains(group))
                        {
                            groups.Add(group);
                            //if (recursive)
                            //AttributeValuesMultiString(attributeName, en.Current.ToString(), valuesCollection, true);
                        }
                    }
                }
            }

            foreach (string name in groupName)
            {
                Console.WriteLine("\t{0}", name);
            }

            foreach (string g in groups)
            {
                string query = groupName.Find(group => group == g.ToUpper());
                if (query != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #11
0
        private static List <string> GetChildDomainPaths(DirectoryEntry directoryEntry)
        {
            var memberPaths = new List <string>();
            PropertyValueCollection memberValueCollection = directoryEntry.Properties[ActiveDirectoryProperties.Member];

            if (memberValueCollection != null)
            {
                IEnumerator memberEnumerator = memberValueCollection.GetEnumerator();
                while (memberEnumerator.MoveNext())
                {
                    if (memberEnumerator.Current != null)
                    {
                        memberPaths.Add(AdDomainPathHandler.Escape(memberEnumerator.Current.ToString()));
                    }
                }
            }

            return(memberPaths);
        }
        public static List <string> GetPropertyValues(string name, object values)
        {
            List <string> propValues = new List <string>();

            PropertyValueCollection pvc       = (PropertyValueCollection)values;
            IEnumerator             pvcValues = pvc.GetEnumerator();

            while (pvcValues.MoveNext())
            {
                Type   type     = pvcValues.Current.GetType();
                String valueStr = GetPropertyValueString(pvcValues.Current);
                if (valueStr != null)
                {
                    propValues.Add(valueStr);
                }
            }

            return(propValues);
        }
Beispiel #13
0
        static List <string> attributeValuesMultiString(string attributeName, string objectDN, List <string> valuesCollection, bool recursive, string prefix, Regex regex)
        {
            DirectoryEntry          entry           = new DirectoryEntry(objectDN);
            PropertyValueCollection valueCollection = entry.Properties[attributeName];
            IEnumerator             enumerator      = valueCollection.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Current != null)
                {
                    string groupName = prefix + enumerator.Current.ToString();

                    if (regex != null)
                    {
                        if (regex.IsMatch(enumerator.Current.ToString()))
                        {
                            groupName = prefix + regex.Match(enumerator.Current.ToString()).Groups[1].Value;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (!valuesCollection.Contains(groupName))
                    {
                        valuesCollection.Add(groupName);

                        if (recursive)
                        {
                            attributeValuesMultiString(attributeName, "LDAP://" +
                                                       enumerator.Current, valuesCollection, true, prefix, regex);
                        }
                    }
                }
            }

            entry.Close();
            entry.Dispose();
            return(valuesCollection);
        }
Beispiel #14
0
        // Recursive
        private CaseIgnoringSortedSetType AttributeValuesMultiString(string attributeName, DirectoryEntry objectEntry, CaseIgnoringSortedSetType valuesCollection, bool recursive)
        {
            PropertyValueCollection ValueCollection = objectEntry.Properties[attributeName];
            IEnumerator             enumerator      = ValueCollection.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Current != null)
                {
                    if (!valuesCollection.Contains(enumerator.Current.ToString()))
                    {
                        valuesCollection.Add(enumerator.Current.ToString());
                        if (recursive)
                        {
                            AttributeValuesMultiString(attributeName, LdapPrefix + enumerator.Current.ToString(), valuesCollection, true);
                        }
                    }
                }
            }
            return(valuesCollection);
        }
Beispiel #15
0
        /// <summary>
        /// Enumerate Multi-String Attribute Values of an Object
        /// This method includes a recursive flag in case you want to recursively dig up properties of properties such as
        /// enumerating all the member values of a group and then getting each member group's groups all the way up the tree.
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="objectDn"></param>
        /// <param name="valuesCollection"></param>
        /// <param name="recursive"></param>
        /// <returns></returns>

        public static ArrayList AttributeValuesMultiString(
            string attributeName,
            string objectDn,
            ArrayList valuesCollection,
            bool recursive)
        {
            string                  path            = "LDAP://" + objectDn;
            DirectoryEntry          ent             = new DirectoryEntry(path);
            PropertyValueCollection ValueCollection = ent.Properties[attributeName];

            //string[] propNames = new string[ent.Properties.PropertyNames.Count];
            //ent.Properties.PropertyNames.CopyTo(propNames, 0);

            //object[] propValues = new object[ent.Properties.Values.Count];
            //ent.Properties.Values.CopyTo(propValues, 0);

            IEnumerator en = ValueCollection.GetEnumerator();             // note: limited to 1500 entries

            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);
                        }
                    }
                }
            }
            ent.Close();
            ent.Dispose();
            return(valuesCollection);
        }
        private IEnumerable <ObjectValues> setPropertyValue(string LDAPPath, string propertyName, string propertyValue, bool secureConnection)
        {
            DirectoryEntry objRootDSE;

            if (secureConnection)
            {
                objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password, AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing);
            }
            else
            {
                objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
            }

            setSinglePropertyValue(objRootDSE, propertyName, propertyValue);

            objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
            //Multi-Value Property
            if (objRootDSE.Properties[propertyName].Count > 1)
            {
                PropertyValueCollection ValueCollection = objRootDSE.Properties[propertyName];
                IEnumerator             en = ValueCollection.GetEnumerator();

                while (en.MoveNext())
                {
                    if (en.Current != null)
                    {
                        yield return(new ObjectValues(LDAPPath, propertyName, en.Current.ToString()));
                    }
                }
            }
            //Single Value Property
            else
            {
                propertyValue = getSinglePropertyValue(objRootDSE, propertyName);
                yield return(new ObjectValues(LDAPPath, propertyName, propertyValue));
            }
        }
        public override RecordPropertyValueCollection GetPropertyValues(Record rec, RecordProperty prop)
        {
            Converter <string, string> getSourceValue = null;
            DirectoryEntry             mainItem       = rec.MainItem as DirectoryEntry;
            SearchResult relItem = rec.RelItem as SearchResult;
            ResultPropertyValueCollection rvals = (relItem == null) ? null : relItem.Properties[prop.Name];
            PropertyValueCollection       pvals = (mainItem == null) ? null : mainItem.Properties[prop.Name];
            object obj1 = rec.MainItem;
            SPUser user = null;

            if (prop.Name == "roxVcardExport")
            {
                return(new RecordPropertyValueCollection(this, rec, prop, new object[] { UserDataSource.GetVcardExport(rec) }, null, null, null));
            }
            if (prop.Name == "roxSiteGroups")
            {
                string        str;
                List <object> list = new List <object>();
                if (getSourceValue == null)
                {
                    getSourceValue = pn => rec[pn, string.Empty];
                }
                if (!string.IsNullOrEmpty(str = Record.GetSpecialFieldValue(this, "rox___pl", getSourceValue)))
                {
                    try
                    {
                        user = SPContext.Current.Web.AllUsers[str];
                    }
                    catch
                    {
                    }
                    if (user != null)
                    {
                        foreach (SPGroup group in ProductPage.TryEach <SPGroup>(user.Groups))
                        {
                            try
                            {
                                list.Add(group.Name);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                return(new RecordPropertyValueCollection(this, rec, prop, list.ToArray(), null, null, null));
            }
            if (rvals != null)
            {
                return(new RecordPropertyValueCollection(this, rec, prop, null, rvals.GetEnumerator(), () => rvals.Count, delegate {
                    IEnumerator enumerator = rvals.GetEnumerator();
                    {
                        while (enumerator.MoveNext())
                        {
                            return enumerator.Current;
                        }
                    }
                    return null;
                }));
            }
            if (pvals != null)
            {
                return(new RecordPropertyValueCollection(this, rec, prop, null, pvals.GetEnumerator(), () => pvals.Count, () => pvals.Value));
            }
            return(null);
        }
 public IEnumerator GetEnumerator()
 {
     return(_pc != null ? _pc.GetEnumerator() : _rc.GetEnumerator());
 }
        private IEnumerable <ObjectValues> getValues(string LDAPPath, bool secureConnection, bool includeChildItems)
        {
            DirectoryEntry objRootDSE;

            if (secureConnection)
            {
                objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password, AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing);
            }
            else
            {
                objRootDSE = new DirectoryEntry(LDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
            }

            foreach (string strAttrName in objRootDSE.Properties.PropertyNames)
            {
                //Multi-Value Property
                if (objRootDSE.Properties[strAttrName].Count > 1)
                {
                    PropertyValueCollection ValueCollection = objRootDSE.Properties[strAttrName];
                    IEnumerator             en = ValueCollection.GetEnumerator();

                    while (en.MoveNext())
                    {
                        if (en.Current != null)
                        {
                            yield return(new ObjectValues(LDAPPath, strAttrName, en.Current.ToString()));
                        }
                    }
                }
                //Single Value Property
                else
                {
                    string propertyValue = getSinglePropertyValue(objRootDSE, strAttrName);
                    yield return(new ObjectValues(LDAPPath, strAttrName, propertyValue));
                }
            }
            if (includeChildItems)
            {
                var children = objRootDSE.Children;
                foreach (DirectoryEntry child in children)
                {
                    using (child)
                    {
                        foreach (string strAttrName in child.Properties.PropertyNames)
                        {
                            //Multi-Value Property
                            if (child.Properties[strAttrName].Count > 1)
                            {
                                PropertyValueCollection ValueCollection = child.Properties[strAttrName];
                                IEnumerator             en = ValueCollection.GetEnumerator();

                                while (en.MoveNext())
                                {
                                    if (en.Current != null)
                                    {
                                        yield return(new ObjectValues(child.Path, strAttrName, en.Current.ToString()));
                                    }
                                }
                            }
                            //Single Value Property
                            else
                            {
                                string propertyValue = getSinglePropertyValue(child, strAttrName);
                                yield return(new ObjectValues(child.Path, strAttrName, propertyValue));
                            }
                        }
                    }
                }
            }
        }