News

My Stats

  • Posts - 4
  • Comments - 4
  • Trackbacks - 0

Twitter












Recent Comments


Recent Posts


Archives


Post Categories



I have, over the past 2 weeks, been developing on a system which should use Active Directory as it's user source.  That is to say, it get's the usernames from AD and stores them in it's own database with a custom password for security.

Now, when I first started work on this system, there were already a couple of hundred users set up, mostly using the correct username structure, but some which weren't.  The requirement was to ensure that all users had a standard username which matched their AD username, however site security was to be handled by the application rather than Active Directory.

To throw another spanner in the works, the application had been developed back in 2007, by a contractor who, obviously, wasn't with the company any more.  It hadn't been touched since 2007 and it was developed in VB.net... 2.0.

Having never developed against AD before, I did some research and found that there weren't too many good examples about, so I had to dig out my VB.Net bible.

I needed to get a list of all users in the directory, so I could populate a DropDownList to ensure the username was in the correct format whenever a new user was created.  The code to do this is as follows:

C#

public DirectoryEntry GetDirectoryObject()
{

DirectoryEntry oDE = new DirectoryEntry(LDAP://YOUR DOMAIN);
return oDE;

}

public SearchResultCollection GetUsers()
{

DirectoryEntry entry = GetDirectoryObject();
entry.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher deSearch = new DirectorySearcher(entry);

deSearch.Filter = "(&(objectCategory=person)(objectClass=user))";

SearchResultCollection results = deSearch.FindAll();

return results;

}

private void LoadDropdowns()
{

foreach (SearchResult searchResult in DataObjects.Users.GetUsers()) {

DirectoryEntry directoryEntry = default(DirectoryEntry);
directoryEntry = searchResult.GetDirectoryEntry;

if (directoryEntry.SchemaClassName.ToString().ToLower() == "user") {

IEnumerable query = null;
bool userExists = false;
query = users.AsEnumerable().Where(user => user("username").Equals(directoryEntry.Properties("sAMAccountName").Value.ToString));

if (directoryEntry.Properties("sAMAccountName").Value.ToString() != directoryEntry.Properties("CN").Value.ToString()) {

listItem = new ListItem();
listItem.Value = directoryEntry.Properties("sAMAccountName").Value.ToString();
listItem.Text = directoryEntry.Properties("sAMAccountName").Value.ToString() + " (" + directoryEntry.Properties("CN").Value.ToString() + ")";
ddUserName.Items.Add(listItem);

}

}

}

}
 

VB.Net:
Public Shared Function GetDirectoryObject() As DirectoryEntry

Dim oDE As DirectoryEntry

oDE = New DirectoryEntry("LDAP://YOUR DOMAIN")

Return oDE

End Function

Public Shared Function GetUsers() As SearchResultCollection

Dim entry As New DirectoryEntry
entry = GetDirectoryObject()
entry.AuthenticationType = AuthenticationTypes.Secure

Dim deSearch As New DirectorySearcher(entry)
deSearch.Filter = "(&(objectCategory=person)(objectClass=user))"

Dim results As SearchResultCollection
results = deSearch.FindAll()
Return results


End Function

Private Sub LoadDropdowns()

For Each searchResult As SearchResult In DataObjects.Users.GetUsers()

Dim directoryEntry As DirectoryEntry
directoryEntry = searchResult.GetDirectoryEntry

If directoryEntry.SchemaClassName.ToString().ToLower() = "user" Then

Dim query As IEnumerable
Dim userExists = False
query = users.AsEnumerable().Where(Function(user) user("username").Equals(directoryEntry.Properties("sAMAccountName").Value.ToString))

If directoryEntry.Properties("sAMAccountName").Value.ToString() <> directoryEntry.Properties("CN").Value.ToString() Then

listItem = New ListItem
listItem.Value = directoryEntry.Properties("sAMAccountName").Value.ToString()
listItem.Text = directoryEntry.Properties("sAMAccountName").Value.ToString() + " (" + directoryEntry.Properties("CN").Value.ToString() + ")"
ddUserName.Items.Add(listItem)

End If

End If

Next

End Sub

I'll go into AD Development further over the next few days/weeks as I think it's pretty funky!


Comments

Gravatar # re: Development with Active Directory Part 1
Posted by DoniG on 11/18/2010 1:58 PM
If you are using .NET 3.5 or greater, you can use the System.DirectoryServices.AccountManagment classes to make your life a whole lot easier. There was an article in MSDN Magazine in January 2008 (http://msdn.microsoft.com/en-us/magazine/cc135979.aspx).

Doni
Gravatar # re: Development with Active Directory Part 1
Posted by Rob on 11/18/2010 2:02 PM
Doni, Thanks for the comment, I should have mentioned that I was developing against .net 2.0 :(
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: