Solved

Get OU from C# in AD

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 40
Setting runtime form location 4 18
Loop through Multiple Processes Async 2 15
XML & .net 5 16
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…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

18 Experts available now in Live!

Get 1:1 Help Now