Get parent OU of user in Active Directory using C#

active-directory, c#

Solution

Ok @Empi solution is working, but `UserPrincipal` is built on `DirectoryEntry` objects that provides a `parent` or `container` properties that just give you the object you are looking for, without using string way.

/* Retreiving a principal context
 */
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "dom\\jpb", "MyPwd");

/* Retreive a user
 */
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, "user1");

/* Retreive the container
 */
DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry;
DirectoryEntry deUserContainer = deUser.Parent;
Console.WriteLine (deUserContainer.Properties["distinguishedName"].Value);

Problem

I want to check, if a a user is in a specific parent OU. How can I do that? Check below code for a clear desciption of what I am looking for. ``` using System.DirectoryServices.AccountManagement; public bool IsUserInOU(string samAccountName, string OUName){ using (var context = new PrincipalContext(ContextType.Domain)) { using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) { //Check if the user is in the OU specified in OUName //Something like: //return user.IsInOU(OUName); } } } public void TestIt_1(){ //The parent OU of this user is "AwesomeOU" string samAccountName = "Joe"; string OUName = "AwesomeOU"; bool expected = true; bool actual = IsUserInOU(samAccountName, OUName); Assert.AreEqual(expected, actual); } public void TestIt_2(){ //The parent OU of this user is "WhateverOU" string samAccountName = "Mike"; string OUName = "AwesomeOU"; bool expected = false; bool actual = IsUserInOU(samAccountName, OUName); Assert.AreEqual(expected, actual); } ``` The Domain: - National OU - Awesome OU - Joe - Whatever OU - Mike Solution 1 after empi's answer With the information given by empi, I wrote the below method to extract the first OU in the DistinguishedName. Having done that, the rest is a breeze. ``` public static string GetOUForUser(string samAccountName) { using (var context = new PrincipalContext(ContextType.Domain)) { using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) { //System.Console.WriteLine(user.DistinguishedName); int startIndex = user.DistinguishedName.IndexOf("OU=", 1) + 3; //+3 for length of "OU=" int endIndex = user.DistinguishedName.IndexOf(",", startIndex); var group = user.DistinguishedName.Substring((startIndex), (endIndex - startIndex)); return group; } } } ``` Solution 2 after JPBlanc's answer ``` public static string GetOUForUser(string samAccountName) { using (var context = new PrincipalContext(ContextType.Domain)) { using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) { using (DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry) { using (DirectoryEntry deUserContainer = deUser.Parent) { return deUserContainer.Properties["Name"].Value.ToString(); } } } } } ```

Original source