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 8621661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:52:07+00:00 2026-06-12T06:52:07+00:00

I am getting crazy here, I’d really appreciate some help! simply I want to

  • 0

I am getting crazy here, I’d really appreciate some help!
simply I want to get user name or anything from Active Directory using DirectoryEntry class.

I used userprinciple and it works great, but the property I need to get (user’s manager) is only avaliable in DirectoryEntry.

My problem is, I looked so much online and I got the codes from there, but for some reason it never works, always return Null. here is an example :

public static DirectoryEntry GetUser(string UserName)
{
    //create an instance of the DirectoryEntry
    DirectoryEntry de = new DirectoryEntry("LDAP://" + "OU=AnotherOU,OU=xx,OU=Testvironments,DC=abc,DC=local");

    //create instance fo the direcory searcher
    DirectorySearcher deSearch = new DirectorySearcher(de);

    deSearch.SearchRoot = de;
    //set the search filter
    deSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))";
    //deSearch.SearchScope = SearchScope.Subtree;

    //find the first instance
    SearchResult results = deSearch.FindOne();

    //if found then return, otherwise return Null
    if (results != null)
    {
        //de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
        //if so then return the DirectoryEntry object
        return results.GetDirectoryEntry();
    }
    else
    {
        return null;
    }
}

I have no clue why this code returns null.

Thanks in advance.

  • 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-12T06:52:08+00:00Added an answer on June 12, 2026 at 6:52 am

    You can try like this

    //create instance for directory entry
    DirectoryEntry de = new DirectoryEntry("LDAP://" + "OU=AnotherOU,OU=xx,OU=Testvironments,DC=abc,DC=local");
    
    //create instance fo the directory searcher
    DirectorySearcher deSearch = new DirectorySearcher(de );;
    
    //set the search filter
    deSearch.Filter = "(&(objectClass=user)(|(SAMAccountName=" + UserName+ ")(givenName=" + UserName+ ")(name=" + UserName+ ")(SN=" + UserName+ "))";
    
    //find the first instance
    SearchResult results = deSearch.FindOne();
    
    //if found then return, otherwise return Null
    if (results != null)
    {
        //The desired property you want , you can extract in this way.
       DomainName = results .Properties["SamAccountName"][0].ToString();
       return domainName
    }
    else
    {
        return null;
    }
    

    Hope this is what you are looking for.

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

Sidebar

Related Questions

I'm getting kinda crazy over here because I already wasted two hours getting my
I'm getting crazy with a small problem here, I keep getting an error and
I'm getting crazy with this problem: I have got some Oracle SQL-Reports to redesign
Strange one here, hoping to get some feedback to point me in the right
I am going a tad crazy here. I keep getting this error: ImportError: No
Please help me to fix this issue (Im getting crazy now!), My project is
I'm going crazy here is my access SQL but I keep getting the awesome
Iam getting Crazy with JQuery Uploadify V3.1. // setup fileuploader $(#file_upload).uploadify({ 'swf': 'flash/uploadify.swf', 'uploader'
I'm getting crazy with .htaccess and rewrite rule. I'd like to understand how it
I'm getting crazy with this IE 7... ==> hhttp://neu.emergent-innovation.com/ Why does following function not

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.