Join 136,169 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,876 people online right now. Registration is fast and FREE... Join Now!
First of all, hello! I have a project where I would like to interface with Active Directory and started it off following this and after alot of alterations I managed to get it working. I currently have a form with 3 text boxes (1 for server, 1 for username, 1 for password) and then the text box that link wants and the listbox for displaying the users groups. If I enter an actual user it works. However it crashes when I enter a user that doesnt exist. Here is the code I have for the Find Groups button:
CODE
private void button1_Click(object sender, EventArgs e) { string strUserADsPath = "LDAP://" + txtServer.Text + "/cn=" + textBox1.Text + ",ou=Accounts,dc=Gotham,dc=local"; DirectoryEntry oUser; oUser = new DirectoryEntry(strUserADsPath,txtUserName.Text,txtPassword.Text); listBox1.Items.Add("Groups to which " + oUser.Name + " belongs:"); // Invoke IADsUser::Groups method. object groups = oUser.Invoke("Groups"); foreach (object group in (System.Collections.IEnumerable)groups) { // Get the Directory Entry. DirectoryEntry groupEntry = new DirectoryEntry(group); listBox1.Items.Add(groupEntry.Name); } }
Im guessing I need some kind of :
IF (no user found) THEN AlertBox of some kind saying "No user found!" ELSE The above code
I just not sure the correct way to lay it out. Would someone be so kind to help me with this? Once I have a single example that would work in this situation I think I would be ok as I go on experimenting and adding to this. I just havent been able to find what I need so far, maybe because I dont know how to phrase my searches yet.
For active directory lookups it's unusual to know their adsPath explicitly. User entries are often spread out across organizational units. Thats why most examples use a search mechanism instead.
Here's some code I use for this.
First, a simple class to hold the user info:
CODE
public class UserInfo { public string UserName; public string DisplayName; public List<string> Groups = new List<string>(); public bool IsValid = false; public UserInfo(string userName) { this.UserName = userName; } }
Now, a factory to get users from Active Directory:
CODE
public class UserInfoFactory { protected DirectoryEntry rootEntry;
Thank you VERY much baavgai! This gives me alot to mess around with and learn! I especially like how with your ver I dont have to type in the users full name!
Using what you gave me above I managed to create a couple variables and get it to display givenName and sn on thier own lines before the groups. However I cant seem to get it to display parent or dn. Do I need to do something special to get the others?
However I cant seem to get it to display parent or dn.
I don't know about parent, that should be fine. Using the debugger, pause the code after you get the DirectoryEntry and take a look at what's in there. That may give an idea of what's going on. It is concievable you don't have right to the parent, if you're not a domain admin.
Dn is kind of neat. Microsoft, in their wisdom, don't use alias "dn" and prefer the longer version. Look to the property "distinguishedName". Here's some simple code to see what else is hiding in the properties.
CODE
public void DumpEntryProps(DirectoryEntry entry) { foreach (PropertyValueCollection pvc in entry.Properties) { Debug.WriteLine(pvc.PropertyName + "(" + pvc.Count + ")"); for (int i = 0; i < pvc.Count; i++) { object obj = pvc[i]; if (obj != null) { Debug.WriteLine("\t" + obj.ToString()); } } } }
It should be noted that using the "memberOf" property you can also write a get groups function. I didn't even know DirectoryServices implement a "Groups" method, albeit in a round about way. This is some code I've used for years. You must first find the PropertyValueCollection for "memberOf" and pass that:
CODE
protected string[] GetGroups(ICollection pvc) { List<string> list = new List<string>(); if ( (pvc!=null) && (pvc.Count>0) ) { foreach(string groupName in pvc) { int equalsIndex = groupName.IndexOf("=", 1); int commaIndex = groupName.IndexOf(",", 1); if (equalsIndex!=-1) { string roleName = groupName.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1); list.Add(roleName); } } } return list.ToArray(); }
Also, for a real user authentication system, I use this as my user object:
Yeah im using the admin account to read the info and parent doesnt work. odd.. However distinguishedName does. I was really just wanting the OU the account was in. Might have to try some stuff with distinguishedName .. Thanks!
I havent had alot of experience in C#, only took an intro class, lol I am probobly jumping in a little deep going straight into AD stuff but I have a tool I want to make for AD so figure this should be the best way to learn