Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Reading Active Directory

authentication

  • Please log in to reply
No replies to this topic

#1 Parabola

Parabola

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 221 posts

Posted 07 May 2010 - 05:44 AM

This program actually came to pass because I was writing a different program, I didn't know anything about reading Active Directory. The other program I was writing needed some security, to keep normal users from running it. So I set out to learn how to read Active Directory to find out if a user had administrative privileges on the network, and ended up making this program. True, I could just use active directory, but on our network, it has to be accessed through Citrix, and well.... I don't like having to log in to Citrix everytime I want to access AD. Now I can quickly and easily get information on a user.

So here goes:

First off, yes, this is a windows form application. I hope that if you are trying to do this task, you know how to make one, and create your text boxes and what not, So I'm going to skip the GUI setup, as you can make it look however you want. Here's a quick look at mine though:

http://forum.codecal...30&d=1273237822

That strange button on the top right <--> is from another program I have, TeamViewer (great app, look into it), so don't worry about that.

So anyways, lets get to the code:

Whatever you name your form, leave is public alone:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ADInfo()[/SIZE]
[SIZE=2]{ [/SIZE]
[SIZE=2] InitializeComponent();[/SIZE]
[SIZE=2]}[/SIZE]

Now, of course, we need to have a connection to Active Directory, so let's create one:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectoryEntry[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] createDirectoryEntry()[/SIZE]
[SIZE=2]{[/SIZE]
[INDENT][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// create AD connection[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectoryEntry[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ldapConnection = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectoryEntry[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"*******.org"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]ldapConnection.Path = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"LDAP://DC=*******,DC=org"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]ldapConnection.AuthenticationType = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]AuthenticationTypes[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Secure;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ldapConnection;[/SIZE]
[/INDENT][SIZE=2]}[/SIZE]

Of course, the *'s are not really what you put in, it has to do with your server. I put those there because well.... The company may get upset if I posted their LDAP connection string on the net lol.

Now, what does our button do when we click it? Let's see:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] btnUpdate_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)[/SIZE]
[SIZE=2]{[/SIZE]
[INDENT][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] username = txt_username.Text;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sam = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (username == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]   username = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].UserName;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (username.Contains([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]   sam = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]getInfo(username, sam);[/SIZE]
[/INDENT][SIZE=2]}[/SIZE]

So, first off, it grabs the user name we are searching for from txt_username (or whatever you call it). Now, if that user name is empty, then we will use the currently logged in user (most likely yourself), which we get by using Environment.UserName. Also, there's a quick if statement. You might wonder why. I put this here for one reason: Sometimes, I know the person's actual name, sometimes I only know their login ID. By checking the username for a ",", then we know what is being searched for. for example, an ID such as admin or jtu1132 would be the samaccountname, where as a name such as Last, First, would be the cn (or possible just name). By using that statement, the program will differentiate between the 2 searches, and modify accordingly in the next section.

getInfo(username, sam);
This is the part that actually does the work. So let's take a look at that code.
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getInfo([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] username, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sam)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectoryEntry[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myLdapConnection = createDirectoryEntry();[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectorySearcher[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] search = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectorySearcher[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](myLdapConnection);[/SIZE]
[SIZE=2]search.SearchRoot = myLdapConnection;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (sam)[/SIZE]
[SIZE=2]search.Filter = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"(samaccountname="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + username + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]")"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (!sam)[/SIZE]
[SIZE=2]search.Filter = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"(cn="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + username + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]")"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mgr, member = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x, y;[/SIZE]
[SIZE=2]ResetAll();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]try[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SearchResult[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] result = search.FindOne();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (result != [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]ResultPropertyCollection[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] fields = result.Properties;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ldapField [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] fields.PropertyNames)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myCollection [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] fields[ldapField])[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"distinguishedname"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (myCollection.ToString().Contains([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Admin"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]txt_admin.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"YES"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"displayname"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_name.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"samaccountname"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_login.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"logoncount"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_logon.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"company"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_comp.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"telephonenumber"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_phone.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"department"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_dept.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"manager"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]mgr = myCollection.ToString().Remove(0, 3).Replace([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\\"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]x = mgr.IndexOf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]",OU"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],0);[/SIZE]
[SIZE=2]txt_mgr.Text = mgr.Remove(x);[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"company"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_comp.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"streetaddress"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_sAdd.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"title"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_title.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"homedirectory"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_home.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"whencreated"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_create.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mail"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_email.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"description"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txt_desc.Text = myCollection.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ldapField == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"memberof"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]member = myCollection.ToString().Remove(0, 3).Replace([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\\"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]y = member.IndexOf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]",OU"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0);[/SIZE]
[SIZE=2]txt_member.Text = txt_member.Text + member.Remove(y) + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine;[/SIZE]
[SIZE=2]}[/SIZE]
 
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[SIZE=2]tbError.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"No User Found"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Exception[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ex)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]tbError.Text = ex.Message;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]

(Ok, I tried to indent everything properly, and it just didn't want to work. Can't use tab of course, since that takes you to the next field, and spaces are removed when posting, so... sorry about the layout.)

So, we use our AD connection:
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectoryEntry[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myLdapConnection = createDirectoryEntry();[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectorySearcher[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] search = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DirectorySearcher[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](myLdapConnection);[/SIZE]
[SIZE=2]search.SearchRoot = myLdapConnection;[/SIZE]

and then we set up our search:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (sam)[/SIZE]
[SIZE=2]search.Filter = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"(samaccountname="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + username + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]")"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (!sam)[/SIZE]
[SIZE=2]search.Filter = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"(cn="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + username + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]")"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]

This is where the whole username vs. ID comes in. But then we have to do something with the search, so what the next large section of code does is go through each field and if it is something we want, then we populate a box with it.
But you may notice this:
[SIZE=2]member = myCollection.ToString().Remove(0, 3).Replace([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\\"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]y = member.IndexOf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]",OU"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0);[/SIZE]
[SIZE=2]txt_member.Text = txt_member.Text + member.Remove(y) + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine;[/SIZE]
What's all that about?
Well, AD returns member of (and manager, and other items as well) like this:
memberof CN=Group,OU=SomeOU,OU=Another OU,blah=blahblah
Manager will do: CN=Last\, First,OU=Blah blah blah....
Hence the Replace("\\","") to remove the \
And all we want is Group. That little snippet is there to filter all that crud out and give us what we want.
Also, the memberof has well... multiple entries, which is why that is actually dumped into either a RichTextBox or a multiline TextBox.

Now that little ResetAll() function towards the beginning simply clears the result fields from the previous search, nothing more:
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ResetAll()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]tbError.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_admin.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NO"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_mgr.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_comp.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_create.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_dept.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_desc.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_email.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_home.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_login.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_logon.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_member.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_name.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_phone.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_sAdd.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_title.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]txt_username.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]

Also, we need our includes, so:
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Collections.Generic;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.ComponentModel;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Data;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Text;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Microsoft.Win32;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.Mail;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.DirectoryServices;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Runtime.InteropServices;[/SIZE]

There are a couple extra, a little something I'm working on to get my other program to email the user when it is done running (It installs about 10 - 15 programs, automating the whole thing, then restarts the computer- sometimes takes over an hour to run, in which case you don't want to sit and wait)
System.DirectoryServices- You may need to actually add the assembly reference for that, I did. But, I also wrote this program to run on .NET 2.0 because the other program is used on a freshly installed machine, which may not have .NET 3.5 or above.

I would include the source files for this, but I'm sorry- company info contained within, for security reasons I can't.:rules:

But, I would be happy to answer any questions- I know when I first started working with AD, I was lost completely....

Attached Thumbnails

  • Active Directory .JPG

  • 0
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.





Also tagged with one or more of these keywords: authentication

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download