通过Node.js上的Azure函数获取当前经过身份验证的用户名

问题描述 投票:1回答:1

检索O365 / AzureAD用户的详细信息(例如在Node上使用Azure功能的用户名的最佳方法是什么?

仅允许在线身份验证的用户使用Azure功能

我看到的所有示例都与C#上的Azure函数有关。

azure-active-directory azure-functions
1个回答
1
投票

[如果您使用的是Azure AD和node.js,最简单的方法是查看X-MS-CLIENT-PRINCIPAL-NAME HTTP请求标头。这是我的测试代码,供您参考。

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)+". CurrentUser:"+req.headers['x-ms-client-principal-name']
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

enter image description here

参考:

Advanced usage of authentication and authorization in Azure App Service

© www.soinside.com 2019 - 2024. All rights reserved.