Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get OU from C# in AD

Posted on 2012-04-13
4
Medium Priority
?
953 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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