【发布时间】:2019-06-19 11:20:17
【问题描述】:
我目前正在为我的应用设置带有 MSAL 的 ADD。我遇到的问题是 api 设置为接受 Azure AD V1 令牌,但使用我当前的 MSAL 设置,我一直收到 Azure AD V2。
我团队中的其他人正在使用 ADAL,但我们想迁移到 MSAL。我确定我做错了什么,因为似乎很难相信没有向后兼容性。
这是我的 Msal 配置:
import * as Msal from 'msal';
export const applicationConfig = {
clientID: process.env.REACT_APP_MSAL_CLIENT_ID,
authority: process.env.REACT_APP_AUTHORITY_TENANT,
graphScopes: ['user.read'],
graphEndpoint: process.env.REACT_APP_GRAPH_ENDPOINT,
};
/**
* will get the call back once the API is complete
* (either complete or failure), redirects flows.
* Is called after the authentication request is completed
* successfully/failure
*
* @param {*} errorDesc
* @param {*} token
* @param {*} error
* @param {*} tokenType
*/
const tokenReceivedCallback = async (errorDesc, token, errorMsg) => {
try {
if (token) console.log('Success!');
} catch (error) {
throw new Error(`${errorMsg}:${errorDesc}`);
}
};
/**
* Instantiate UserAgentApplication
*/
const userAgentApplication = new Msal.UserAgentApplication(
applicationConfig.clientID,
applicationConfig.authority,
tokenReceivedCallback,
{
cacheLocation: process.env.REACT_APP_CACHE_LOCATION,
redirectUri: process.env.REACT_APP_REDIRECT_URI,
},
);
/**
* Log user in
* Checks if there is no user and if there is no
* callback occuring within the window url which throws into
* infinite loop, then login, and redirect to SSO login
* @param {*} graphScopes
*/
export const signIn = async graphScopes => {
console.log(graphScopes);
/**
* avoid duplicate code execution on page load in case of iframe and popup window
*/
if (!userAgentApplication.getUser() && !userAgentApplication.isCallback(window.location.hash)) {
/**
* login site, and go directly to SSO
*/
await userAgentApplication.loginRedirect(graphScopes, process.env.REACT_APP_DOMAIN);
/**
* acquireTokenSilent method makes a silent request to ADD to obtain an access token.
* ADD returns an access token containing the user consented scopes to allow
* the app to securely call the api
*/
await userAgentApplication.acquireTokenSilent(graphScopes);
}
};
/**
* Logs user out
*/
export const logOut = () => userAgentApplication.logout();
提前致谢!
【问题讨论】:
-
这些范围不是 Graph API 而不是你的 API 吗?您在哪里为您的 API 请求令牌?
-
嗨@juunas 感谢您的快速回复。我不确定我是否遵循您问题的第一部分。我认为范围适用于graphAPI,
loginRedirect()属于MSAL()对象。我向graph.windows请求我的令牌 -
您正在为 Graph API 请求访问令牌。那不是您的 API,因此它不尊重您的令牌版本设置。您需要更改范围以匹配您的 API 标识符。
-
github.com/AzureAD/microsoft-authentication-library-for-dotnet/… 向您展示了一些示例。您通常会使用 app id URI + /.default 或某些特定范围。如果您想为客户端和 API 使用相同的应用注册,我不记得是否有所不同。
-
如果 API 尚不存在,您可以从 Azure 门户创建应用 ID URI
标签: azure authentication azure-active-directory adal msal