Accessing objects in active directory via asp.net


T

Toufani

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
 
Ad

Advertisements

J

Joe Kaplan \(MVP - ADSI\)

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top