You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express.js后端用LDAPJS分配用户组时遇操作及对象不存在错误

Express.js中LDAPS实现用户组分配报错求助及方案咨询

我在Express.js后端通过LDAPS(3.0.3)开发给用户分配组的功能,核心逻辑写在updateUserGroup()函数里。跟着教程操作后遇到了如下错误,翻了LDAPJS文档也没找到相关解决办法:

"error": "Operations Error"

使用的技术栈:Express.js、ldapjs模块

另外,如果有人知道在Express.js中对Active Directory执行CRUD操作的更好方案,欢迎分享。

代码片段

async function setupclient(): Promise<ldap.Client> {
   const client = ldap.createClient({
      url: 'LDAPS://example.com:636',
      tlsOptions: {
         rejectUnauthorized: true,
         cert: fs.readFileSync('x.pem'),
         ca: fs.readFileSync('xx.pem'),
      },
      reconnect: false,
   });

   const username = process.env.LDAP_USERNAME;
   const password = process.env.LDAP_PASSWORD;
   try {
      await new Promise((resolve, reject) => {
         client.bind(username, password, (err) => {
            if (err) {
               console.error('LDAP bind error:', err);
               reject(new Error(`LDAP bind error: ${err}`));
            } else {
               console.log('Connected to LDAP');
               resolve(true);
            }
         });
      });
   } catch (err) {
      client.unbind();
      throw err;
   }

   return client;
}
async function updateUserGroup({ pid, dn, gn }: UpdateUser): Promise<any> {
   try {
      const client = await setupclient();

      const change = new ldap.Change({
         operation: 'add',
         modification: {
            type: 'member',
            values: ['CN=sara,OU=Portal,OU=Users,OU=Perm2,OU=Bassmor,DC=example,DC=com'],
         },
      });
      return new Promise((resolve, reject) => {
         client.modify(
            'CN=SuperCoolUsers,OU=Portal,OU=Accrom,OU=Portal,OU=Groups,OU=Perm2,OU=Bassmor,DC=example,DC=com',
            change,
            (err) => {
               if (err) {
                  console.log(err);
                  reject(new Error(err.message));
               requests特别 vsCalsAdamsblob规格CL-outest, replacesLike(null);
                  resolve(null);
               }
            }
         });
      });
   } catch (error: any) {
      console.error('updateUserGroup error:', error);
      throw new Error(error.message);
   }
}

补充说明

感谢之前的建议,我最初确实是按正确方式写的,但上传了错误的代码片段,现在已经更新为正确版本。修改后出现了新错误:"No Such Object"。但我可以确定DN路径是正确的——这个路径是从Windows的ADSI-Edit里复制出来的,而且ADSI-Edit和我的Express.js后端用的是相同凭据,连接的也是同一台LDAPS服务器。

ADSI-Edit显示的DN信息

(注:为保护隐私,真实域名的DC部分已替换为example)


内容的提问来源于stack exchange,提问作者docker compose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:53:16