Пример #1
0
        void verifyAttributes(LdapConnection con, string dn, Dictionary <string, string> attributes)
        {
            SearchResultAttributeCollection attrs = getAttributes(
                con,
                dn,
                attributes.Keys.ToArray());

            if (attrs == null)
            {
                return;
            }

            Dictionary <string, string> .Enumerator enumer = attributes.GetEnumerator();
            while (enumer.MoveNext())
            {
                if (!attrs.Contains(enumer.Current.Key) && enumer.Current.Value != null)
                {
                    errors.Add("Failed to find attribute " + enumer.Current.Key + " from object " + dn);
                }
                else
                {
                    string value = attrs[enumer.Current.Key].ToString().ToLower();
                    if (enumer.Current.Value.ToLower() != value)
                    {
                        errors.Add("expected: " + enumer.Current.Value.ToLower() + " , actual: " + value + ", attribute " + enumer.Current.Key + " of object " + dn);
                    }
                }
            }
        }
        // Token: 0x0600103A RID: 4154 RVA: 0x0004EB34 File Offset: 0x0004CD34
        public static DirectoryAttribute GetRangedPropertyValue(ADPropertyDefinition propertyDefinition, SearchResultAttributeCollection attributeCollection, out IntRange returnedRange)
        {
            DirectoryAttribute result = null;

            returnedRange = null;
            string   text;
            IntRange propertyRangeFromLdapName = RangedPropertyHelper.GetPropertyRangeFromLdapName(propertyDefinition.LdapDisplayName, out text);

            if (attributeCollection.Contains(propertyDefinition.LdapDisplayName))
            {
                returnedRange = propertyRangeFromLdapName;
                result        = attributeCollection[propertyDefinition.LdapDisplayName];
            }
            else
            {
                string attributeNameWithRange = ADSession.GetAttributeNameWithRange(text, propertyRangeFromLdapName.LowerBound.ToString(), "*");
                if (attributeCollection.Contains(attributeNameWithRange))
                {
                    returnedRange = new IntRange(propertyRangeFromLdapName.LowerBound, int.MaxValue);
                    result        = attributeCollection[attributeNameWithRange];
                }
                else
                {
                    string value = string.Format(CultureInfo.InvariantCulture, "{0};{1}{2}-", new object[]
                    {
                        text,
                        "range=",
                        propertyRangeFromLdapName.LowerBound
                    });
                    foreach (object obj in attributeCollection.AttributeNames)
                    {
                        string text2 = (string)obj;
                        if (text2.StartsWith(value, StringComparison.OrdinalIgnoreCase))
                        {
                            result        = attributeCollection[text2];
                            returnedRange = RangedPropertyHelper.GetPropertyRangeFromLdapName(text2, out text);
                        }
                    }
                }
            }
            return(result);
        }
        public void AttributeCollection_CaseInsensitivity()
        {
            var searchCollection = new SearchResultAttributeCollection();
            var modifyCollection = new ModifyAttributeCollection();
            var gnAttr           = new DirectoryAttribute {
                Name = LdapAttributes.GivenName.ToLowerInvariant()
            };
            var modifAttr = new DirectoryModificationAttribute {
                Name = LdapAttributes.GivenName.ToLowerInvariant()
            };

            searchCollection.Add(gnAttr);
            modifyCollection.Add(modifAttr);

            Assert.True(searchCollection.Contains(LdapAttributes.GivenName.ToUpperInvariant()));
            Assert.True(modifyCollection.Contains(LdapAttributes.GivenName.ToUpperInvariant()));
        }
Пример #4
0
        private DirectoryAttribute ReadMultivalueAttribute(string absolutePath, string attributeName)
        {
            SearchRequest request = new SearchRequest(absolutePath, Schema.Query.QueryAll, System.DirectoryServices.Protocols.SearchScope.Base, new string[]
            {
                attributeName
            });
            SearchResponse searchResponse = (SearchResponse)this.SendRequest(request);

            if (searchResponse.Entries.Count > 0)
            {
                SearchResultAttributeCollection attributes = searchResponse.Entries[0].Attributes;
                if (attributes.Contains(attributeName))
                {
                    return(attributes[attributeName]);
                }
            }
            return(null);
        }
Пример #5
0
        private string GetSearchResultEntryValue(SearchResultEntry entry, string attributeName)
        {
            SearchResultAttributeCollection attributes = entry.Attributes;

            if (!attributes.Contains(attributeName))
            {
                return(null);
            }

            try
            {
                return(string.Join(",", attributes[attributeName].GetValues(typeof(string))));
            }
            catch
            {
                return(null);
            }
        }
Пример #6
0
        private object ReadSingleObjectAttribute(string absolutePath, string attributeName, params DirectoryControl[] controls)
        {
            SearchRequest searchRequest = new SearchRequest(absolutePath, Schema.Query.QueryAll, System.DirectoryServices.Protocols.SearchScope.Base, new string[]
            {
                attributeName
            });

            foreach (DirectoryControl control in controls)
            {
                searchRequest.Controls.Add(control);
            }
            SearchResponse searchResponse = (SearchResponse)this.SendRequest(searchRequest);

            if (searchResponse.Entries.Count > 0)
            {
                SearchResultAttributeCollection attributes = searchResponse.Entries[0].Attributes;
                if (attributes.Contains(attributeName))
                {
                    return(attributes[attributeName][0]);
                }
            }
            return(null);
        }
Пример #7
0
        // ----- CONSTRUCTORS -----

        /// <summary>
        /// Establishes a connection with an LDAP server that can be used to query or modify its contents.
        /// <param name="servers">A list of servers by fully qualified domain name, host name, ip address, or null.</param>
        /// <param name="portNumber">The port number on the LDAP server that is listening for requests.</param>
        /// <param name="authType">(Optional) The type of authentication to use when connecting with the server. By default this is set to Anonymous (i.e. no credentials required).</param>
        /// <param name="userName">(Optional) The user name to use when connecting to the LDAP server.</param>
        /// <param name="password">(Optional) The password to use with the user name provided to connect to the LDAP server.</param>
        /// <param name="domainName">(Optional) The domain or computer name associated with the user credentials provided.</param>
        /// <param name="useLogonCredentials">(Optional) If enabled, the LDAP connection will use the logon credentials from the current session. Disabled by default.</param>
        /// </summary>
        public LDAP(List <string> servers, int portNumber, AuthType authType = AuthType.Anonymous, string userName = null, SecureString password = null, string domainName = null, bool useLogonCredentials = false)
        {
            if ((servers != null && servers.Count > 0 && portNumber > 0 && !string.IsNullOrWhiteSpace(userName) && password != null) || (servers != null && servers.Count > 0 && portNumber > 0 && useLogonCredentials))
            {
                try
                {
                    // Setup the server information for the connection.
                    LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier(servers.ToArray(), portNumber, false, false);

                    // Setup the credential to use when accessing the server. (Or null for Anonymous.)
                    NetworkCredential credential = null;
                    if (authType != AuthType.Anonymous)
                    {
                        credential = new NetworkCredential(userName, password);
                        if (!string.IsNullOrWhiteSpace(domainName))
                        {
                            // A domain was provided. Use it when creating the credential.
                            credential.Domain = domainName;
                        }
                    }

                    // Create the connection to the server(s).
                    try
                    {
                        if (useLogonCredentials)
                        {
                            connection = new LdapConnection(directoryIdentifier);
                        }
                        else
                        {
                            connection = new LdapConnection(directoryIdentifier, credential, authType);
                        }


                        // Gather information about the LDAP server(s) from the RootDSE entry.
                        SearchResponse rootDSESearchResponse = (SearchResponse)connection.SendRequest(new SearchRequest(null, "(objectClass=*)", SearchScope.Base));
                        if (rootDSESearchResponse != null && rootDSESearchResponse.ResultCode == ResultCode.Success)
                        {
                            // Save the rootDSE for access by API clients.
                            rootDSE = rootDSESearchResponse.Entries[0];
                            SearchResultAttributeCollection attributes = rootDSE.Attributes;

                            // Check that LDAP V3 is supported.
                            if (attributes["supportedLDAPVersion"].GetValues(typeof(string)).Contains("3"))
                            {
                                // Get all of the naming contexts this server(s) supports.
                                namingContexts = (string[])attributes["namingContexts"].GetValues(typeof(string));

                                // Set the base DN for searching to the first naming context in the list.
                                searchBaseDN = namingContexts[0];

                                // Get any alternate servers can complete our requests should this one stop responding.
                                // If there are not other servers to contact this attribute is not available.
                                if (attributes.Contains("altServer"))
                                {
                                    alternateServers = (string[])attributes["altServer"].GetValues(typeof(string));
                                }
                            }
                            else
                            {
                                throw new NotSupportedException("The directory server does not support LDAP v3.");
                            }
                        }

                        // Bind to the ldap server with the connection credentials if supplied.
                        if (connection.AuthType != AuthType.Anonymous)
                        {
                            connection.Bind();
                        }
                    }
                    catch (System.ComponentModel.InvalidEnumArgumentException)
                    {
                        // Thrown when authType is out of range.
                        throw new ArgumentOutOfRangeException("authType");
                    }
                }
                catch (ArgumentException)
                {
                    throw new ArgumentException("Entries in the servers parameter can not have spaces.");
                }
            }
            else
            {
                if (servers == null || servers.Count == 0)
                {
                    throw new ArgumentNullException("servers", "The list of servers can not be null or empty.");
                }
                if (portNumber <= 0)
                {
                    throw new ArgumentOutOfRangeException("portNumber", "A port number must be positive.");
                }
            }
        }