Accessing objects in active directory via asp.net

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Accessing objects in active directory via asp.net

    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
    Toufani Guest

  2. Similar Questions and Discussions

    1. #40227 [NEW]: COM object issues with ADODB accessing Active Directory
      From: steven dot partridge at l-3com dot com Operating system: Windows 2003 Server SP1 PHP version: 5CVS-2007-01-24 (snap) PHP...
    2. Trouble Accessing Active Directory Domain Controller
      I am having troubles accessing a different Domain Controller than the one I am currently in. Any help would be appreciated. Dave ...
    3. Active Directory Search fails ("The directory service is unavailab
      Hi all, I'm having one of those nerve wrecking errors, when trying to perform a simple search in an Active Directory. The objective of the code...
    4. Accessing Active Directory
      Hello there I've got a little problem when trying finding the person behind a global address book entry. Some background infos: I have an...
    5. Do you need Framework 1.1 to access Active Directory objects on a Windows 2003 server with ASP.NET?
      Some of our developers are having a problem making certain code work that reads the contents of OUs in our Active Directory. It reads fine when...
  3. #2

    Default Re: Accessing objects in active directory via asp.net

    Do a search on this group in Google for the word tokenGroups and Kaplan to
    see an example of the proper way to retrieve group membership for a user.
    MemberOf is deficient in a number of important ways.

    Joe K.

    "Toufani" <toufani@gmail.com> wrote in message
    news:e3dc3601.0408310430.4d19ee2@posting.google.co m...
    > 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

    Joe Kaplan \(MVP - ADSI\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139