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

时间:2019-12-11 19:14:55

标签: azure-active-directory azure-functions

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

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

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

1 个答案:

答案 0 :(得分: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