Solved

Get OU from C# in AD

Posted on 2012-04-13
4
922 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

16 Experts available now in Live!

Get 1:1 Help Now