Ask a Question related to ASP.NET Security, Design and Development.
-
Toufani #1
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=redex.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
-
#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... -
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 ... -
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... -
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... -
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... -
Joe Kaplan \(MVP - ADSI\) #2
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



Reply With Quote

