获取多个范围的单个令牌 microsoft entra id

问题描述 投票:0回答:1

我有一个 asp.net core web api 项目。该项目使用

azure entra id
代表
authentication
。我在此注册了 2 个应用程序,第一个用于 React 客户端应用程序,第二个用于 asp.net core Web api。该 Web api 项目有 2 个模块,其中一个
mes
受到此范围
"api://********-****-****-****-************/Mes"
的保护,其他
user management
需要上述
scope
加上这些
graph api
范围,

 "User.Read",
 "Directory.ReadWrite.All",
 "User.Invite.All"

现在在 React 应用程序上,我使用

msal-react
msal-browser
进行登录和获取令牌,但是当我尝试为所有这些范围获取单个令牌以便我可以使用单个令牌访问所有 api 时,我只获得令牌对于
mes
范围或
graph api
范围

const loginRequest = {
  scopes: [
    "api://********-****-****-****-************/Mes",
    "User.Read",
    "Directory.ReadWrite.All",
    "User.Invite.All",
  ],
};

const handleLogin = async () => {
  try {
    let auth = await instance.loginPopup(loginRequest);
    localStorage.setItem("user", JSON.stringify(auth));
    localStorage.setItem("access_token", auth.accessToken);
    return auth;
  } catch (error) {
    if (error instanceof AuthError && error.errorCode === 'user_cancelled') {
      console.log('User cancelled the login flow');
      return rejectWithValue('User cancelled the login flow');
    } else {
      console.error('Login error:', error);
      throw rejectWithValue(error.message);
    }
  }
};

当我在登录请求中请求所有范围时,它仅提供

graph api
的令牌,要获取
mes
的令牌,我必须在登录请求中单独请求它。 这个问题的目的是找到一种方法,让我获得所有这些范围的单个令牌。

azure asp.net-core-webapi token msal-react microsoft-entra-id
1个回答
0
投票

简单的答案是你不能。

如果所有范围都属于同一资源,则可以为多个范围获取令牌。例如,在单个请求中,您可以获取

User.Read
Directory.ReadWrite.All
User.Invite.All
范围的令牌,因为它们是 Graph API 资源的一部分。

要获取 API 的令牌,您需要发出单独的请求,因为它是完全不同的资源。

© www.soinside.com 2019 - 2024. All rights reserved.