473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 21 '05 #1
1 3915
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Remco Bosman | last post by:
Dear Developers, I am building a little WebPart for the new sharepoint 2003 services. The webspart simply has to show a list of users and their phonenumbers, which come from our main domain....
1
by: Toufani | last post by:
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...
2
by: Conversão .cs em .dll | last post by:
I'm having a problem accessing Active Directory, the problem is that I need to access the passwords of the user to validate a login that i'm doing. I have access for the usernames but not to there...
2
by: =?Utf-8?B?SklNLkgu?= | last post by:
Accessing AD in ASP.Net 1. It sees I can I access Active Directory catalog from asp.net, is the version of AD important for this? 2. Do I need to have a domain user for that or IIS will be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.