By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Accessing objects in active directory via asp.net

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <to*****@gmail.com> wrote in message
news:e3*************************@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

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.