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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:56:57+00:00 2026-05-14T00:56:57+00:00

I wish to write a reusable library for querying against AD with LDAP. I’m

  • 0

I wish to write a reusable library for querying against AD with LDAP. I’m using both ActiveDs COM objects and System.DirectoryServices.

Greatly inspired by Bart de Smet LINQ to AD, I have written a SchemaAttribute and an DirectoryAttributeAttribute classes to use with a DirectorySource(Of T) class (Yes, it’s VBNET, but any C# code will help as I’m fluent in both languages).

Now, when querying against AD using LDAP (System.DirectoryServices), you may choose what property/attribute you wish to get loaded by the DirectorySearcher class. Then, I’ve written myself a method that takes a ParramArray of String as its parameter, so that I add the LDAP properties to the DirectorySearcher.PropertiesToLoad() method within a foreach() statement. Here’s a piece of code to make it clear (Assuming that ldapProps parameter will always contain value(s)):

Public Function GetUsers(ByVal ParamArray ldapProps() As String) As IList(Of IUser)
    Dim users As IList(Of IUser) = New List(Of IUser)
    Dim user As IUser
    Dim de As DirectoryEntry = New DirectoryEntry(Environment.UserDomainName)
    Dim ds As DirectorySearcher = New DirectorySearcher(de, "(objectClass=user)")

    For Each p As String In ldapProps
        ds.PropertiesToLoad(p)
    Next

    Try
        Dim src As SearchResultCollection = ds.FindAll()
        For Each sr As SearchResult In src
            user = New User()
            // This is where I'm stuck... Depending on the ldapProps required, I will fill only these in my User object.
        Next
End Function

Here’s a piece of my User class:

Friend NotInheritable Class User
    Implements IUser

    Private _accountName As String
    Private _firstName As String

    <DirectoryAttributeAttribute("SAMAccountName")> _
    Public Property AccountName As String
        Get
            Return _accountName
        End Get
        Set (ByVal value As String)
            If (String.Equals(_accountName, value)) Then Return

            _accountName = value
        End Set
    End Property

    <DirectoryAttributeAttribute("givenName")> _
    Public Property FirstName As String
        Get
            Return _firstName
        End Get
        Set (ByVal value As String)
            If (String.Equals(_firstName, value)) Then Return

            _firstName = value
        End Set
    End Property
End Class

Now, I would like to benefit of those attributes that I put on top of my User class properties. I know how to get these attributes, and I know how to get my properties. What I am unsure is how to make sure that the right property will be set the right value from the SearchResult class to my User class.

EDIT As time plays against me, I can’t wait to get acquainted with the concept of DirectorySource(Of T), as it requires a bit more coding for me to write to get it working. As a workaround, I’m writing a UserFactory class which will be called through my ActiveDirectoryFacade.

EDIT This SO question seems to be very close to what I wish to accomplish:
Reflection, Attributes and Property Selection

EDIT This looks like what I want: C# setting property values through reflection with attributes
Anyone has another idea or can confirm this is right?

I shall also mention that I’m stuck in .NET Framework 2.0 and VBNET2005. Otherwise, I would have used Bart de Smet’s LINQ to AD library.

Thanks for any help.

  • 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-14T00:56:58+00:00Added an answer on May 14, 2026 at 12:56 am

    I’m not familiar with DirectoryServices, but if I got your question right you could use reflections to set the properties of your User object. To set the correct properties you should match names of the properties with data saved in the attributes on the properties of the User’s object.

        [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
        public class DirectoryAttributeAttribute : Attribute
        {
            public DirectoryAttributeAttribute(string propertyName)
            {
                PropertyName = propertyName;
            }
    
            public string PropertyName
            {
                get; set;
            }
        }
    
        public class User
        {
            [DirectoryAttributeAttribute("SAMAccountName")]
            public string AccountName
            {
                get; set;
            }
    
            [DirectoryAttributeAttribute("givenName")]
            public string FirstName
            {
                get; set;
            }
        }
    
        // Finds property info by name.
        public static PropertyInfo FindProperty(this Type type, string propertyName)
        {
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                object[] attributes = propertyInfo.GetCustomAttributes(typeof(DirectoryAttributeAttribute, false));
    
                foreach (DirectoryAttributeAttribute attribute in attributes)
                {
                    if (attribute.PropertyName == propertyName)
                    {
                        return propertyInfo;
                    }
                }
            }
    
            return null;
        }
    
        SearchResult searchResult = ...;
    
        if (searchResult != null)
        {
            User user = new User();
    
            Type userType = typeof (User);
    
            foreach (string propertyName in searchResult.Properties.PropertyNames)
            {
                // Find property using reflections.
                PropertyInfo propertyInfo = userType.FindProperty(propertyName);
    
                if (propertyInfo != null) // User object have property with DirectoryAttributeAttribute and matching name assigned.
                {
                    // Set value using reflections.
                    propertyInfo.SetValue(user, searchResult.Properties[propertyName]);
                }
            }
        }
    

    If names of the properties you are going to fill can change you can store mapping of properties in Dictionary.

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

Sidebar

Related Questions

I am using Qt and wish to write a class that will perform some
First, I wish to write myself a generic type for operations against the underlying
I have an XElement instance and I wish to write to a stream using
I'm using constructor injection for the first time and wish to write my code
I wish to write a C program which obtains the system time and hence
I wish to write a simple javascript function that toggles the visibility of a
I wish to add some sort of a Write a Review or Rate Us
I wish to send an email from my localhost machine (using PHPs mail function)
I wish to be able to generate URL variables like this: http://example.com/195yq http://example.com/195yp http://example.com/195yg
The more I develop iPhone apps, the more reusable functions I write. At the

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.