我从 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。
如果用户使用服务端代码登录,是否可以静默获取访问令牌。