Solved

Need To Determine Distinguished Name For An OU Using C#

Posted on 2014-11-10
5
367 Views
Last Modified: 2014-11-16
I need to be able to determine the distinguished name for an Active Directory Organizational Unit (OU) using C#. Once I have it I will then be able to insert it into the below statement which is part of a program to get all groups under an OU. Can someone please provide the code to identify the distinguished name or point to a published solution? Thanks.


    PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OUDistingushedName);


Thanks in advance.
0
Comment
Question by:zonkerman
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40433620
How are you getting the OU?

An OU is by its very nature a DN. An OU can not exists that is not a full path from the root of the domain. As you must know the path to the OU to get the OU in the first place....

How are you referencing the OU in the first place? What would you pass to the function that returns the DN in your mind?
0
 
LVL 1

Author Comment

by:zonkerman
ID: 40434258
I already know the OU I need which is in the variable assignment below for OU_ToSearchForGroupsIn.  It's a somewhat lengthy string in a specific format that I understand but I don't want my user's to have to know the format.  They know the name of the OU where the groups are, which for example in the below would be "Finance Dept"   that is all I want them to have to know.   So this is why I want to know know to derive the full formatted string value based on the OU name.  Can it be done and if so how.  I wish there were a function such as the following:  
public String GetDistinguisedNameForOU(String anOU_Name);

I can already derive the Distinguished names for AD groups under an OU but for some reason the code block I currently use won't work on an OU.


try
   {
     OU_ToSearchForGroupsIn = "OU=Finance Dept,OU=Company Apps,DC=XYZCorp,DC=com
     PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OU_ToSearchForGroupsIn);
   
    GroupPrincipal findAllGroups = new GroupPrincipal(PCTX, "*");
    PrincipalSearcher aPrincipalSearcher = new PrincipalSearcher(findAllGroups);
    foreach (var group in aPrincipalSearcher.FindAll())
    {
     try
     {
      }
    }
0
 
LVL 1

Accepted Solution

by:
zonkerman earned 0 total points
ID: 40434839
I found the answer. Basically I had to just use the DirectorySearcher with the filter shown below.  Doing this gives me the distinguished name for an OU name only.    

    String OU_Name ="Finance Dept";
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectCategory=organizationalUnit)(name=" + OU_Name + "))";

    SearchResult result = mySearcher.FindOne();

    if (result != null)
    {
       DirectoryEntry directoryObject = result.GetDirectoryEntry();
       distinguishedName = directoryObject.Properties ["distinguishedName"].Value.ToString();    
    }

This site needs more bottom line responses rather than lectures on things people are not asking for.
0
 
LVL 1

Author Closing Comment

by:zonkerman
ID: 40445501
I selected my own solution because it works and no one else provided a working solution.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40445564
Your solution is kind of OK but what happens when you have more that one OU somewhere called, for example, "Data Entry" ?

It will fail.

"This site needs more bottom line responses rather than lectures on things people are not asking for."

THAT'S why we ask questions about what you are trying to do and how you are getting there.  What this site needs is people who are willing to engage with an expert when they are asked a question. We don't ask you for more information because we have time to spare and feel like a chat, it's because we would like to provide the questioner with a foolproof, accurate solution, not something you can find on GOOGLE in five minutes that will kind of work sometimes until you put in into production and then it breaks on your users.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now