Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:59:31+00:00 2026-06-03T09:59:31+00:00

Assuming that active directory is set up correctly, I’m trying to find a way

  • 0

Assuming that active directory is set up correctly, I’m trying to find a way to determine whether or not two people are from the same location. The only way I was able to wrap my head around it is to find a way to determine whether or not their directory entry was located in the same OU. So currently, this is what I am spit-balling at the moment:

private bool ComparePeople()
{
    var user1Guid = "aaa";
    var user2Guid = "bbb";
    var expr = @"CN=.*?,";
    var user1OU = Regex.Replace(GetUserDN(user1Guid), expr, string.Empty);
    var user2OU = Regex.Replace(GetUserDN(user2Guid), expr, string.Empty);
    return user1OU == user2OU;
}
private string GetUserDN(string userGuid)
{
    using(var entry = new DirectoryEntry(string.format("LDAP://<GUID={0}>", userGuid)))
    {   
        using(var search = new DirectorySearcher(entry))
        {
            search.PropertiesToAdd.Add("distinguishedName");
            var result = search.FindOne().GetDirectoryEntry();
            if(result != null && result.Properties["distinguishedName"].Count > 0)
            {
                return result.Properties["distinguishedName"].Value.ToString();
            }
            else return "";
        }
    }
}

I haven’t tested this yet, but I feel like it would work. It basically finds the distinguished name of a user, give their Guid. Then it removes the CN from the DN, essentially finding the path to that user’s directory entry/OU. However, it seems a bit convoluted. Does anyone have any comments or recommendations to simplify this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:59:33+00:00Added an answer on June 3, 2026 at 9:59 am

    If I understand you correctly, you’re trying to find out whether two user accounts are located inside the same OU (organizational unit) – right?

    What I would do is read the parent of both user accounts – if that parent matches, then they’re in the same OU.

    If you’re on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context and easily find users and/or groups in AD:

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find a two users
    UserPrincipal user1 = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, user1Guid);
    UserPrincipal user2 = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, user2Guid);
    
    if(user1 != null && user2 != null)
    {
         DirectoryEntry dirEntry1 = user1.GetUnderlyingObject() as DirectoryEntry;
         DirectoryEntry dirEntry2 = user2.GetUnderlyingObject() as DirectoryEntry;
    
         // if both are OK, get the parents and compare their GUID
         if(dirEntry1 != null && dirEntry2 != null)
         {
             DirectoryEntry parent1 = dirEntry1.Parent;
             DirectoryEntry parent2 = dirEntry2.Parent;
    
             bool areInSameOU = (parent1.Guid == parent2.Guid);
         }
    }
    

    The new S.DS.AM makes it really easy to play around with users and groups in AD!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain
I'm trying to set up memcached to store the results of the query that
assuming that I know the PID of a process and want to do a
Assuming that I have a CronTriggerBean similar to <bean id=midMonthCronTrigger class=org.springframework.scheduling.quartz.CronTriggerBean> <property name=jobDetail ref=reminderJobDetail
Assuming that best practices have been followed when designing a new database, how does
Assuming that I have a typedef declared in my .h file as such: typedef
Assuming that I know there is a git-daemon running at git://git.mycompany.com , how can
Assuming that one event has multiple handlers, if any of event handlers throw an
Assuming that CompareAndSwap (or CAS) never fails spuriously, can CompareExchange be implemented with CAS?
Assuming that I cannot run something like this with Fabric: run(svn update --password 'password'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.