adal-node 包中的代理设置

问题描述

我正在尝试使用 adal-node package() 在我的节点服务器中生成令牌以对 Azure AD 进行身份验证。它在我的本地机器上工作,但是当尝试在虚拟机上运行时,它给出以下错误

{ 错误:getaddrinfo EAI_AGAIN login.microsoftonline.com login.microsoftonline.com:443 在 GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26) 错误号:'EAI_AGAIN',代码:'EAI_AGAIN',系统调用:'getaddrinfo', 主机名:'login.microsoftonline.com', 主机:'login.microsoftonline.com',端口:443` }

示例代码片段

var authorityUrl = 'https://login.microsoftonline.com/53d652a4-6f71-416f-971f-*****'
    var applicationId = '26915ab6-22fc-4017-8741-***************'; // Application Id of app registered under AAD.
    var clientSecret = '2NZtB5mIX1xZaXZ_I6I~-*********'; // Secret generated for app. Read this environment variable.
    var resource = 'https://**************.com'; // URI that identifies the resource for which the token is valid.
    var context = new AuthenticationContext(authorityUrl);
    context.acquiretokenWithClientCredentials(resource,applicationId,clientSecret,function(err,tokenResponse) {
        if (err) {
            console.log('well that didn\'t work: ' + err.stack);
            console.log(err);
            res.json(err.stack);
        } else {
           
            console.log(tokenResponse.accesstoken);
           
        }
        

分析:貌似是代理问题,但是不知道如何使用adal-node包设置代理url。请指教。

解决方法

如果你想在你的应用中使用代理服务器,请参考下面的代码

const { AuthenticationContext,Logging } = require("adal-node");
const proxy = require("node-global-proxy").default;
proxy.setConfig({
  http: "",https: ""
});
proxy.start();
const context = new AuthenticationContext(
  "https://login.microsoftonline.com/const { AuthenticationContext,Logging } = require("adal-node");
const proxy = require("node-global-proxy").default;
proxy.setConfig({
  http: "http://149.28.43.184:8080",});
proxy.start();
const context = new AuthenticationContext(
  "https://login.microsoftonline.com/e4c9ab4e-bd27-40d5-8459-230ba2a757fb"
);

Logging.setLoggingOptions({
  log: function (level,message,error) {
    console.log(message);
    if (error) {
      console.log(error);
    }
  },loggingWithPII: false,level: Logging.LOGGING_LEVEL.VERBOSE,});
var applicationId = ""; // Application Id of app registered under AAD.
var clientSecret = ""; // Secret generated for app. Read this environment variable.
var resource = "";
context.acquireTokenWithClientCredentials(
  resource,applicationId,clientSecret,function (err,tokenResponse) {
    if (err) {
      console.log("well that didn't work: " + err.stack);
    } else {
      console.log(tokenResponse);
    }
  }
);

proxy.stop()