首页 > 解决方案 > 我的函数在 Active Directory 中为节点返回未定义

问题描述

有谁知道如何在以下函数中返回身份验证?它目前正在返回 null 。我尝试使用异步等待和其他许多东西,但仍然很难返回任何东西。谢谢您的帮助!

var ActiveDirectory = require('activedirectory');
var config = { url: myurl,
  baseDN: 'CN=Users,DC=usdtl,DC=com',
  username: my-username,
  password: my-pass }
var ad = new ActiveDirectory(config);
ad.authenticate(userPrincipalName, password, (err, auth) => {
  if (err) {
    console.log('ERROR: '+JSON.stringify(err));
    return auth;
  }
  if (auth) {
    console.log(auth)
    return auth
  }
})

标签: node.jsactive-directoryldap

解决方案


这太棒了。我从未听说过异步。有没有其他方法可以从以下类返回回调结果?因为我一直在尝试使异步工作,但它不起作用。我在另一个地方调用这个类函数。

import { Injectable } from '@nestjs/common';

@Injectable()
 export class AuthService {
  async authenticateUser(username, password) {
    let isAuthenticated: boolean = false
    var ActiveDirectory = require('activedirectory');
    var config = { url: 'my_url',
     baseDN: 'CN=Users,DC=usdtl,DC=com' }
    var ad = new ActiveDirectory(config);
   ad.authenticate(username, password, function(err, auth) {
   if (err) {
    console.log('ERROR: '+JSON.stringify(err));
    return err;
  }
  if (auth) {
    console.log('Authenticated!');
    isAuthenticated = auth
  }
});
} 
}

推荐阅读