gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:48:47 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com