Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current authenticated Username via Azure Function on Node.js

What would be the best approach to retrieve O365/AzureAD user's details such as username who consumes an Azure Function on Node?

Only Online authenticated users are allowed to consume the Azure Function

All the examples I see are related to Azure Functions on C#.

like image 917
Ozie Harb Avatar asked Oct 23 '25 15:10

Ozie Harb


1 Answers

If you're using Azure AD and node.js, the easiest way is to look at the X-MS-CLIENT-PRINCIPAL-NAME HTTP request header. Here is my test code for your reference.

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

Reference:

Advanced usage of authentication and authorization in Azure App Service

like image 91
Tony Ju Avatar answered Oct 26 '25 08:10

Tony Ju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!