node.js - 如何使用 adal-node 身份验证上下文验证访问 token ?

标签 node.js azure adal

我使用 adal-node 身份验证上下文和 AngularJS 应用程序创建了一个 Azure Web API,jwt token (访问 token )已通过 AngularJS 应用程序传递以调用 Web API。在允许用户访问 Web API 之前,我需要通过 jwt token 验证用户。如何使用 adal-node 身份验证上下文进行 jwt 验证。

生成访问 token 的示例代码

function getToken(TENANT) {
    var promise = new Promise(function (resolve, reject) {
        try {
            //const authContext = new adal.AuthenticationContext(`https://login.microsoftonline.com/${TENANT}`);
            const authContext = new adal.AuthenticationContext('https://login.microsoftonline.com/'+TENANT);
            authContext.acquireTokenWithClientCredentials(GRAPH_URL,CLIENT_ID,CLIENT_SECRET,function(err,tokenRes)
            {
                if (err)
                {
                    reject(err);
                }
                var accesstoken = tokenRes.accessToken;
                resolve(accesstoken);
            })
        }
        catch (ex) {
            reject(ex);
        };
    });
    return promise;
}

最佳答案

实际上,根据adal-node的文档:

The ADAL for node.js library makes it easy for node.js applications to authenticate to AAD in order to access AAD protected web resources. It supports 3 authentication modes shown in the quickstart code below.

所以,总而言之,adal-node 不具备验证 JWT 作为 IDP 服务器的功能。

但是,如果您想阻止没有权限的人访问您的 Web api。您可以轻松利用 Azure 应用服务的身份验证和授权功能。您可以使用 AAD 来保护您的 Web API,并且针对此 Web API 的所有请求都需要在其 Authorization header 中设置来自 AAD 的访问 token 。

您可以引用Authentication and authorization for API Apps in Azure App Service了解更多信息。

同时,如果您打算自己验证 JWT,则可以利用一些第三方模块,例如https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback

关于node.js - 如何使用 adal-node 身份验证上下文验证访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41931504/

相关文章:

azure - WEBSITE_TIME_ZONE 已设置但未通过应用程序设置

javascript - UnhandledPromiseRejectionWarning 即使有一个 .catch promise

javascript - 使用 Mongoose 通过传递选项 JSON 来过滤数据 DB 级别

azure - 从 Azure for Students 迁移到按需付费

azure - 为什么 Azure 逻辑应用有 'frequency' 设置?

javascript - 使用 OfficeDev/office-js-helpers 而不是 adal 进行身份验证

javascript - Node.js - 如何初始化/定义 req.body?

http - 使用 gzip/deflate 压缩的简单 HTTP 请求

azure - Adal JS - 仅注销一个 AD 站点

python - 无法以编程方式验证用户身份