Solved

Get OU from C# in AD

Posted on 2012-04-13
4
923 Views
Last Modified: 2012-04-13
Hallo Experts,

I am using Active Directory. I need to get output of

whoami /FQDN

command in C#. I can get user informations from

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();

But I want to get the OU also what is given by whoami /FQDN.

Please help

Regards,

Soumen
0
Comment
Question by:Soumen-Roy
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37841921
Try:
var sid = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var user = new System.DirectoryServices.DirectoryEntry(string.Format("LDAP://<SID={0}>", sid.Value));
var cn = user.Properties["distinguishedName"].Value;

Console.WriteLine(cn);

Open in new window

0
 
LVL 10

Expert Comment

by:gavsmith
ID: 37841942
Try having a play with the following code:

        DirectoryEntry searchroot = new DirectoryEntry("LDAP://dc=yourdomainname,dc=com");
        DirectorySearcher ds = new DirectorySearcher(searchroot);
        ds.SearchScope = SearchScope.Subtree;
        ds.Filter = "(&(objectclass=user)(samaccountname="+ wi.Name +"))";
        SearchResultCollection searchResults = ds.FindAll();
        string UserDetails = searchResults [0].GetDirectoryEntry().Properties["distinguishedname"].Value

Open in new window


Using the code above you should be able to get to all the information you require, you may need to change which property you return the value of 'distinguisgedname' gets various info which may be useful to you but there are lots of other properties.

The other thing you may need to look at is passing the windows identity name to the samaccountname filter, you may need to only pass the username and not the domain (i.e 'username' not 'domain\username').

so like I said you will have to play around with it but it should give you what you need.
0
 
LVL 10

Expert Comment

by:gavsmith
ID: 37841961
wdosanjos post wasn't there when I started typing! his solution seems much easier to follow.

Mine was a cut down version of what I use for a address book search facility so probably overkill for what you need.
0
 

Author Closing Comment

by:Soumen-Roy
ID: 37841966
Perfect solution,

Exactly what I expected. Thanx a lot.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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