InvalidDnSyntaxError when I try to create a new AD user with ldapjs

55 views Asked by At

I am attempting to create a user through ldapjs, here is my code:

async onboardUser(targetUser, ou) {
        targetUser.distinguishedName = `cn=${user.username}, ${ou}`;

        return new Promise(function (resolve, reject) {
            const entry = {
                distinguishedName: targetUser.distinguishedName,
                displayName: targetUser.displayName,
                sAMAccountName: targetUser.username, 
                cn: targetUser.displayName,
                name: targetUser.displayName,
                sn: targetUser.lname,
                givenName: targetUser.fname,
                userPrincipalName: targetUser.email, 
                mail: targetUser.email,
                co: targetUser.country,
                company: targetUser.company,
                manager: targetUser.manager['distinguishedName'],
                physicalDeliveryOfficeName: targetUser.city,
                department: targetUser.department,
                title: targetUser.title,
                unicodePwd: Buffer.from('"' + targetUser.password + '"', 'utf16le').toString(),
                objectclass: 'user',
                userAccountControl: 512, //NORMAL_ACCOUNT
            };

            // Create user in AD, error occurs here
            ldapjs.add(targetUser.distinguishedName, entry, (err) => {
                if (err) {
                    logger.error(err.stack);
                    reject(err);
                } else {
                    resolve('success');
                }
            });
        });
    }

However, I receive the following error:

InvalidDnSyntaxError: 00002081: NameErr: DSID-03050F42, problem 2003 (BAD_ATT_SYNTAX), data 0, best match of:
    'cn=awesomeusername, ou=Users, ou=Accounts, dc=company, dc=org'

I am stuck on the above error and do not know how to proceed. Any advice as to what I may be doing wrong is greatly welcomed.

Thank you for your time and attention

1

There are 1 answers

2
ChrisCodesThings On

Try getting rid of the of the spaces after the commas:

targetUser.distinguishedName = `cn=${user.username}, ${ou}`;
//                                                  ^

See MS Documentation: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ldap/distinguished-names

Looks like it should be: 'cn=awesomeusername,ou=Users,ou=Accounts,dc=company,dc=org'