Retrieving User Information from LDAP Active Directory Server with Node.js using ldapjs package

please START READING FROM THE Heading

I can see that the LDAP connection and binding are successful, and the search options look correct. However, the Entry found and User found logs show undefined.

What I have tried so far:

Double-checked the ldapConfig values to ensure they are correct.
Verified that the userPrincipalName being searched exists in the directory.
Logged the search options to confirm they include the correct filter and attributes.
Despite these steps, the user information is still not being fetched. Can anyone help me understand why the user is coming back as undefined and how to fix it?

I’m trying to create a function that fetches all details for a user from the Active Directory using ldapjs in Node.js. Here is the function I’m using:


const ldapConfig = {
    url: 'ldap://your-ldap-server',
    bindDN: 'cn=admin,dc=example,dc=com',
    bindCredentials: 'admin-password',
    searchBase: 'dc=example,dc=com',
    searchOptions: {
        scope: 'sub',
        attributes: ['*'] // Fetch all attributes
    }
};

function searchLDAPByUserPrincipalName(userPrincipalName, callback) {
    const client = ldap.createClient({
        url: ldapConfig.url
    });

    console.log('Connecting to LDAP server...');

    client.bind(ldapConfig.bindDN, ldapConfig.bindCredentials, (err) => {
        if (err) {
            console.error('Bind error:', err);
            return callback(err);
        }

        console.log('Bind successful');

        const opts = {
            ...ldapConfig.searchOptions,
            filter: `(&(objectClass=person)(userPrincipalName=${userPrincipalName}))`
        };

        console.log('Search options:', opts);

        client.search(ldapConfig.searchBase, opts, (err, res) => {
            if (err) {
                console.error('Search error:', err);
                client.unbind();
                return callback(err);
            }

            let user = null;

            res.on('searchEntry', (entry) => {
                console.log('Entry found:', JSON.stringify(entry.object, null, 2));
                user = entry.object;
            });

            res.on('searchReference', (referral) => {
                console.log('Referral:', referral.uris.join());
            });

            res.on('error', (err) => {
                console.error('Search entry error:', err);
                client.unbind();
                return callback(err);
            });

            res.on('end', (result) => {
                console.log('Search end status:', result.status);
                client.unbind();
                if (result.status !== 0) {
                    return callback(new Error(`Non-zero status from LDAP search: ${result.status}`));
                }
                callback(null, user);
            });
        });
    });
}

// Example usage
searchLDAPByUserPrincipalName('[email protected]', (err, user) => {
    if (err) {
        console.error('LDAP search failed:', err);
    } else {
        console.log('User found:', user);
    }
});```


When I run this code, I expect to see the user details in the log. However, I'm getting the following log output:
my-formsrc> node server.js
Connecting to LDAP server...
Bind successful
Search options: {
  scope: 'sub',
  attributes: [ '*' ],
  filter: '(&(objectClass=person)([email protected]))'
}
Entry found: undefined
Search end status: 0
User found: undefined

I can see that the LDAP connection and binding are successful, and the search options look correct. However, the Entry found and User found logs show undefined.

What I have tried so far:

Double-checked the ldapConfig values to ensure they are correct.
Verified that the userPrincipalName being searched exists in the directory.
Logged the search options to confirm they include the correct filter and attributes.
Despite these steps, the user information is still not being fetched. Can anyone help me understand why the user is coming back as undefined and how to fix it?

New contributor

Abdul. A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật