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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:40:54+00:00 2026-05-31T01:40:54+00:00

How can i get the IADs interface of an Active Directory user – by

  • 0

How can i get the IADs interface of an Active Directory user – by username?

Note: Native code


i am trying to write the function that can get the IADs interface of a user in Active Directory.

i have the following “pseudocode” so far:

public IADs GetUserObject(string samAccountName)
{
   IADs ads;

   //Get the current domain's distinguished name ("dc=stackoverflow,dc=com")
   AdsGetObject("LDAP://rootDSE", IADs, ref ads);
   String dn = ads.Get("defaultNamingContext"); //"dc=stackoverflow,dc=com"

   String path;

   //Attempt #1 to bind to a user by username
   path = "LDAP://sSAMAccountName="+samAccountName+",dc=stackoverflow,dc=com"
   AdsGetObject(path, IADs, ref ads); //invalid syntax

   return ads;       
}

The trick, that i cannot figure out, is how to bind to the user by their account name. The following variantions don’t work:

  • LDAP://sSAMAccountName=ian,dc=stackoverflow,dc=com
  • LDAP://dc=stackoverflow,dc=com;(&(objectCategory=user)(sAMAccountName=ian))
  • <LDAP://dc=stackoverflow,dc=com>;(&(objectCategory=user)(sAMAccountName=ian))

Edit:

A version that does work, but doesn’t answer my question, is:

  • LDAP://cn=Ian Boyd,ou=Avatar Users,dc=stackoverflow,dc=com

It doesn’t answer my question for two reasons:

  • i don’t know the user’s CN (Common-Name) (e.g. Ian Boyd), only their sAMAccountName (e.g. ian)
  • doesn’t work for users not in the Avatar Users organizational unit; and i don’t know a user’s OU

Which comes from the notes i had before:

Note:

  • i don’t know the name of the domain (but that’s okay, i can get it at runtime)
  • i don’t know the name of any active directory servers
  • i don’t know the folder that the user is in

tl;dr: How would you write the utility function:

public IADs GetUserObject(string samAccountName)
{
   //TODO: ask stackoverflow
}

Update 2:

Note:

  • i already know how to query for information about a user using .NET’s DirectorySearcher
  • i already know how to query for information about a user using the Active Directory Services OLEDB provider with ADO (using the SQL syntax, but not the native syntax)
  • i’m now looking for code to query for information about a user using Active Directory Services COM objects (hence the question about getting an IADs for a user)

Update 3:

It certainly might require me to apply “filters“, except i don’t know where. The only ActiveDs interface that mentions Filter is IADSContainer, but i don’t know where to get one.

i tried randomly to get the IADsContainer interface from the root IADs interface, but “rootDSE” doesn’t support IADsContainer:

IADs ads = AdsGetObject("LDAP://rootDSE");
IADsContainer container = (IADsContainer)ads; //interface not supported exception

i could

  • ask a question on how to get the IADsContainer of the AD root
    • so i can ask how to recursively search active diretory
      • so i can ask how to filter using IADsContainer
        • so i can figure out how to get the IADs object of a user
          • so i an figure out how to query AD for user properties

But keeping track of all these questions is difficult.

  • 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-05-31T01:40:56+00:00Added an answer on May 31, 2026 at 1:40 am

    If you know the value of sAMAccountName and need to get IADs of the user you need first find the user in Active Directory by the sAMAccountName and get distinguishedName attribute of the user. You know already how to get IADs by distinguishedName.

    So you should just follow the code from MSDN for example. First you get IDirectorySearch interface of the AD container of defaultNamingContext of "LDAP://rootDSE".

    IADs domain;
    ADsGetObject("LDAP://rootDSE", IADs, domain);
    

    Then you use IDirectorySearch::ExecuteSearch to apply search using the filter string:

    (&(objectClass=user)(objectCategory=person)(sAMAccountName=theName))
    

    Note: The search filter syntax is described here.

    IDirectorySearch directorySearch = domain as IDirectorySearch;
    ADS_SEARCH_HANDLE searchHandle;
    
    directorySearch.ExecuteSearch(
          "(&(objectClass=user)(objectCategory=person)(sAMAccountName=ian))",
          attributeNames, numberOfAttributes,
          out searchHandle);
    
    • you use the known value of sAMAccountName instead of theName.

    • for pAttributeNames you can use LPOLESTR array which consist from L"distinguishedName" only (see pszNonVerboseList from the code example and look the code of FindUsers in case of bIsVerbose as FALSE).

    You should get distinguishedName attribute of first (and the only if any exist) found item. Having distinguishedName attribute you can use AdsGetObject to get the IADs of the user.

    Alternatively you can get objectGUID attribute of the user instead of distinguishedName attribute and use binding by GUID syntax, but the usage of distinguishedName I personally find more clear and understandable.


    public IADs GetUserObject(string samAccountName)
    {
       IADs ads;
    
       //Get the current domain's distinguished name (e.g. "dc=stackoverflow,dc=com")
       AdsGetObject("LDAP://rootDSE", IADs, ref ads);
       String dn = ads.Get("defaultNamingContext"); //"dc=stackoverflow,dc=com"
    
       //Get the the object of the current domain (e.g. LDAP://dc=stackoverflow,dc=com)
       AdsGetObject("LDAP://"+dn, IADs, ref ads);
    
       //Now we're going to search for the "distinguishedName" of this user
    
       //setup the search filter for the user we want
       String filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName="+samAccountName+"))";
    
       //specify that we only need to return one attribute, distinguishedNamem, 
       //otherwise it returns all attributes and is a waste of resources
       String[] searchAttributes = { "distinguishedName" };
    
       //run the search
       IDirectorySearch ds = ads as IDirectorySearch;
       ADS_SEARCH_HANDLE searchHandle;
       ds.ExecuteSearch(filter, searchAttributes, 1, out searchHandle);
       ds.GetFirstRow(searchHandle);
    
       //Now get the details of the "distinguishedName" column
       ADS_SEARCH_COLUMN column;
       ds.GetColumn(searchHandle, "distinguishedName", ref column);
    
       //Get the user's distinguishedName
       String dn = column.pADsValues.DNString;
    
    
       //Now that we have the user's distinguishedName, we can do what we really wanted:
       AdsGetObject("LDAP://"+dn, IADs, ads);
    
       return ads;
    }
    

    This means that conceptually it can be broken down into two steps:

    • getting a user’s distinguishedName from their samAccountName
    • fetching the IADs for a distinguishedName

    And splitting the code:

    public IADs GetUserObject(string samAccountName)
    {
       String userDistinguishedName = GetUserDistinguishedName(samAccountName);
    
       return GetObject("LDAP://"+userDistingishedName);
    }
    
    public String GetUserDistinguishedName(string samAccountName)
    {
       //Get the current domain's distinguished name (e.g. "dc=stackoverflow,dc=com")
       IADs ads = GetObject("LDAP://rootDSE");
       String dn = ads.Get("defaultNamingContext"); //"dc=stackoverflow,dc=com"
    
       //Get the the object of the current domain (e.g. LDAP://dc=stackoverflow,dc=com)
       ads := GetObject("LDAP://"+dn);
    
       //Now we're going to search for the "distinguishedName" of this user
    
       //setup the search filter for the user we want
       String filter = '(&(objectClass=user)(objectCategory=person)(sAMAccountName='+samAccountName+'))';
    
       //specify that we only need to return one attribute, distinguishedNamem, 
       //otherwise it returns all attributes and is a waste of resources
       String[] searchAttributes = { "distinguishedName" };
    
       //run the search
       IDirectorySearch ds = ads as IDirectorySearch;
       ADS_SEARCH_HANDLE searchHandle;
       ds.ExecuteSearch(filter, searchAttributes, 1, out searchHandle);
       ds.GetFirstRow(searchHandle);
    
       //Now get the details of the "distinguishedName" column
       ADS_SEARCH_COLUMN column;
       ds.GetColumn(searchHandle, "distinguishedName", ref column);
    
       //Get the user's distinguishedName
       return column.pADsValues.DNString;
    }          
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the code of a stored procedure using the syscomments table. select
I can get my page's html code during my program and what I want
I can get the first record back from the code below in SQL Server
I can get the view function from request.path : from django.core.urlresolvers import resolve view_func,
Can I get a MAC address that are connected to my site. this code
I can get current date of iphone via this code as NSDate *currentDate =
Can I get the objects created in Interface builder in xcode by program ?
I can get the user name as User.Identity.Name . How can I get a
Can get all triples with value null in specific field? All people with date_of_birth

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.