首页 > 解决方案 > 是否可以使用 MSAL 获取 Azure AD V1 令牌?

问题描述

我目前正在为我的应用程序使用 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();

这是我使用 jwt.ms 时得到的: 在此处输入图像描述

提前致谢!

标签: azureauthenticationazure-active-directoryadalmsal

解决方案


是的,可以从 V2 端点请求 V1 访问令牌。颁发给客户端应用程序的访问令牌类型(v1 或 v2)由资源 API 的应用程序注册决定。正如其他人指出的那样,您的示例代码请求 Microsoft Graph 范围,并且 Microsoft Graph 应用程序注册配置为接受 v2 令牌。您可以通过查看应用注册来确定您的 API 配置为接受哪种类型的令牌。在portal.azure.com中,打开“ App registrations (Preview) ”,转到“ Manifest ”部分,然后查找属性“ accessTokenAcceptedVersion ”。如果设置为null1,那么所有请求访问令牌以调用此资源的客户端应用程序都将获得一个 v1 访问令牌(无论它们使用 MSAL 还是 ADAL 来请求访问令牌)。

ADAL 应用调用 v1 资源 API 的常见调用模式是提供资源 URI 作为范围。这告诉端点为资源 API 的应用程序注册中配置的所有权限颁发访问令牌。MSAL(使用 v2 端点)允许请求任何范围,无论它是否位于资源的应用注册范围的静态列表中。要获得与 ADAL(使用 v1 端点)相同的行为,请将“.default”附加到资源 URI(例如“ https://contoso.onmicrosoft.com/V1TodoListService/.default ”)

在 Ignite,我演示了一个仅接受 v1 访问令牌的现有待办事项列表服务,我浏览了我的新 MSAL 客户端应用程序的整个门户配置以请求 v1 访问令牌并调用此服务。在这里观看:https ://youtu.be/77A47CfNqIU?t=3120


推荐阅读