Accessing objects in active directory via asp.net

From: Toufani (toufani_at_gmail.com)
Date: 08/31/04


Date: 31 Aug 2004 05:30:13 -0700

Hi everybody,

I want to retrieve information about objects in active directory
windows 2000 and their properties. I got some codes that don't work
absolutely. for example I can't retrieve users list and group list
separatedly.there is my code that downloaded from the internet :

public class LdapAuthentication
        {
                private string _path;
                private string _filterAttribute;

                public LdapAuthentication(string path)
                {
                        _path = path;
                }

                public bool IsAuthenticated(string domain, string username,
string pwd)
                {
                        String domainAndUsername = domain + @"\" + username;

                        DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
                                        
                        try
                        {
                                //Bind to the native AdsObject to force authentication.
                                Object obj = entry.NativeObject;
                
                                 DirectorySearcher search = new DirectorySearcher(entry);
                
                                search.Filter = "(SAMAccountName=" + username + ")";
                                search.PropertiesToLoad.Add("cn");
                                SearchResult result = search.FindOne();
                
                                if(null == result)
                                {
                                        return false;
                                }
                
                                //Update the new path to the user in the directory.
                                _path = result.Path;
                                _filterAttribute = (String)result.Properties["cn"][0];
                        }
                        catch (Exception ex)
                        {
                                throw new Exception("Error authenticating user. " + ex.Message);
                        }
                
                        return true;

                }

                public string GetRoles( )
                {
                        DirectorySearcher search = new DirectorySearcher(_path);
        
                        search.Filter = "(objectClass=group)";
                        search.PropertiesToLoad.Add("member");
                        StringBuilder roleNames = new StringBuilder();
                        try
                        {
                                SearchResult result = search.FindOne();
                                int propertyCount = result.Properties["member"].Count;
                                String dn;
                                int equalsIndex, commaIndex;

                                for( int propertyCounter = 0; propertyCounter <
propertyCount;propertyCounter++)
                                {
                                        dn = (String)result.Properties["member"][propertyCounter];

                                        equalsIndex = dn.IndexOf("=", 1);
                                        commaIndex = dn.IndexOf(",", 1);
                                        if (-1 == equalsIndex)
                                        {
                                                return null;
                                        }
                                        roleNames.Append(dn.Substring((equalsIndex + 1), (commaIndex -
equalsIndex) - 1));

                                        roleNames.Append("|");
                                }
                        }
                        catch(Exception ex)
                        {
                                throw new Exception("Error obtaining group names. <font color=red
>" +
                                        ex.Message+"</font>");
                        }
                        
                        return roleNames.ToString();
                }

                public string GetGroups()
                {
                        DirectorySearcher search = new DirectorySearcher(_path);
                        search.Filter = "(cn=" + _filterAttribute + ")";
                        search.PropertiesToLoad.Add("memberOf");
                        StringBuilder groupNames = new StringBuilder();
                        try
                        {
                                SearchResult result = search.FindOne();
                                int propertyCount = result.Properties["memberOf"].Count;
                                String dn;
                                int equalsIndex, commaIndex;

                                for( int propertyCounter = 0; propertyCounter < propertyCount;
                                        propertyCounter++)
                                {
                                        dn = (String)result.Properties["memberOf"][propertyCounter];

                                        equalsIndex = dn.IndexOf("=", 1);
                                        commaIndex = dn.IndexOf(",", 1);
                                        if (-1 == equalsIndex)
                                        {
                                                return null;
                                        }
                                        groupNames.Append(dn.Substring((equalsIndex + 1),
                                                (commaIndex - equalsIndex) - 1));
                                        groupNames.Append("|");
                                }
                        }
                        catch(Exception ex)
                        {
                                throw new Exception("Error obtaining group names. " +
                                        ex.Message);
                        }
                        return groupNames.ToString();
                }

In fact, I don't know which filter is appropriate for retrieve
information about groups (ofcourse, I got some result by setting my
active directory path ,_path , but it is not thing that i want). I
examine filters above.
please tell me about :

1- search.Filter
2- "objectClass=group"
3- PropertiesToLoad.Add
4- NativeObject
5- and the way to get groups and their members,users and their
properties

So thanks



Relevant Pages

  • Accessing objects in active directory via asp.net
    ... I want to retrieve information about objects in active directory ... I got some codes that don't work ...
    (microsoft.public.dotnet.general)
  • Re: Need assistance badly!
    ... I have tried cobbling together a script that does this, ... I would use ADO in a VBScript program to retrieve all users with the ... Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN ... adoConnection.Open "Active Directory Provider" ...
    (microsoft.public.scripting.vbscript)
  • Re: domain name/LDAP redundancy
    ... The app currently contacts one of the DC's for the LDAP search however, ... uses ADO to retrieve the Distinguished Names of all users in the domain. ... adoConnection.Open "Active Directory Provider" ...
    (microsoft.public.windows.server.active_directory)
  • Re: Bulk unlock user accounts
    ... following script from some sample on the Microsoft site. ... It makes more sense to retrieve distinguishedName. ... Dim strDN, objUser ... ' Use ADO to search Active Directory. ...
    (microsoft.public.scripting.vbscript)
  • Re: Add domain user to local group script troubleshoot
    ... The most reliable way to retrieve computer names in an Active Directory ... domain global group a member of each computers local Administrators group. ... >> Microsoft MVP Scripting and ADSI ...
    (microsoft.public.windows.server.scripting)