2

@azure/msal-node 我有一个使用库进行用户登录的 nodejs/expressjs 应用程序。为此,我遵循了官方的 Microsoft 文档

用户登录就像一个魅力。

但现在我必须访问用户的个人资料,最重要的是用户名。我尝试了很多解决方案,但没有任何效果。

例如,在 中,我按照这里 的建议进行了server.js尝试。但这不起作用,在我在这里链接的线程中,有人说它只适用于版本“^0.2.4”。试过这个,但 npm 找不到这个版本。this.app.getUser();this.app.getAccount();

正如这个答案中所建议的,我还在 Azure AD 中为我的应用程序设置了family_namegeneral_name,但我不知道如何从令牌中获取信息。因为在 Chrome 开发者工具中,没有发送包含这些信息的 Token。

这是我到目前为止所拥有的:

// server.js
const path = require('path');

const express = require("express");
const msal = require('@azure/msal-node');

const SERVER_PORT = process.env.PORT || 3000; 
const REDIRECT_URI = "http://localhost:3000"; 

const config = {
    auth: {
        clientId: "xyz",
        authority: "xyz",
        clientSecret: "xyz"
    },
    system: {
        loggerOptions: {
            loggerCallback(loglevel, message, containsPii) {
                console.log(message);
            },
            piiLoggingEnabled: false,
            logLevel: msal.LogLevel.Verbose,
        }
    }
};

// Create msal application object
const pca = new msal.ConfidentialClientApplication(config);

// Create Express App and Routes
const app = express();

app.use(express.static(path.join(__dirname, './build')));

app.get('/login', (req, res) => {
    const authCodeUrlParameters = {
        scopes: ["user.read"],
        redirectUri: REDIRECT_URI,
    };

    // get url to sign user in and consent to scopes needed for application
    pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
        console.log("my response: ", response);
        // Here I tried pca.getUser(); and pca.getAccount();
        res.redirect(response);
    }).catch((error) => console.log(JSON.stringify(error)));
});

app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`))

由于我使用express.jsReact 作为前端:我应该更好地使用msal-react吗?我想避免这种情况,因为这不是微软官方支持的。

4

0 回答 0