4

我从 GitHub 下载了示例以试验 Azure AD B2C https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi

我已经为我的 AADB2C 重新配置了这个 Web 应用程序并且一切正常。

为了还试验 MSAL.js,我添加了一个新页面,该页面实现了登录用户的获取访问令牌。用户使用服务器端代码登录。但是,我无法为登录的用户获取缓存的用户会话,因为 MSAL.js 似乎不知道用户已经从服务器端代码登录,反之亦然。

这是我用来获取登录用户会话并尝试以静默方式获取令牌的代码。

if (msalInstance.getAccount()) {
    var tokenRequest = {
        scopes: ["user.read", "mail.send"]
    };
    msalInstance.acquireTokenSilent(tokenRequest)
        .then(response => {
            // get access token from response
            // response.accessToken
        })
        .catch(err => {
            // could also check if err instance of InteractionRequiredAuthError if you can import the class.
            if (err.name === "InteractionRequiredAuthError") {
                return msalInstance.acquireTokenPopup(tokenRequest)
                    .then(response => {
                        // get access token from response
                        // response.accessToken
                    })
                    .catch(err => {
                        // handle error
                    });
            }
        });
} else {
    // user is not logged in, you will need to log them in to acquire a token
}

即使用户已经使用 Microsoft.Identity Code (MSAL C# lib) 登录,msalInstance.getAccount() 函数也将始终返回 null。

如果用户使用服务端代码登录,是否可以静默获取访问令牌。

4

1 回答 1

0

有同样的问题。奇怪的是,当您的 .js 应用程序采用在您的 .net 应用程序中启动的会话时,开发人员 Msal.js 存在安全问题。尽管客户端不是放置这种安全机制的正确位置。这是我找到该信息的地方:github上的相关问题

您可以通过将会话 id 或 login_hint 添加到 acquireTokenSilent() 来在您的 .js 应用程序中采用 .net-started 会话和 msal.js。更多信息可以在这里找到:官方 msal.js 文档

于 2019-12-05T14:57:12.270 回答