Solved

Get OU from C# in AD

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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