msal acquireTokenSilent 失败,静默身份验证被拒绝

vci*_*ial 5 graph azure-ad-msal

我正在使用 msal-Angular,但无法使用 MsalInterceptor,因为它处理每个请求,而我希望它仅处理图形请求。因此,我试图在我的应用程序中自己获取令牌。

我这样做:MsalModule 的配置

MsalModule.forRoot({
  auth: {
    clientId: 'xxxxxx',
    authority: 'https://login.microsoftonline.com/common',
    redirectUri: 'my_url',
  },
  cache: {
    cacheLocation: 'localStorage',
    storeAuthStateInCookie: false, // set to true for IE 11
  },
},
  {
    popUp: true,
    consentScopes:[
      'user.read',
      "calendars.read",
      "calendars.read.shared",
      "calendars.readwrite",
      "group.read.all",
      "openid",
      "profile"
    ],
  }),
Run Code Online (Sandbox Code Playgroud)

我正在做这样的登录:

this.msalService.loginPopup({scopes: [
  'user.read',
  "calendars.read",
  "calendars.read.shared",
  "calendars.readwrite",
  "group.read.all",
  "openid",
  "profile"
]})
Run Code Online (Sandbox Code Playgroud)

我像这样拦截登录成功

this.broadcastService.subscribe('msal:loginSuccess' ...
Run Code Online (Sandbox Code Playgroud)

我正在尝试获得这样的令牌

const result: AuthResponse = await this.msalService.acquireTokenSilent({scopes: [
  'user.read',
  "calendars.read",
  "calendars.read.shared",
  "calendars.readwrite",
  "group.read.all",
  "openid",
  "profile"
]}).catch((error) => {
  console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

我仍然收到此错误:静默身份验证被拒绝。用户必须首先登录,并根据需要授予客户端应用程序对“user.read calendars.read calendars.read.shared calendars.readwrite group.read.all openid profile”范围的访问权限。

这是我在 Azure 门户上的应用程序配置

在此输入图像描述

我实在不明白问题从何而来。

感谢您的帮助

[编辑] 当 acquireTokenSilent 失败时,我已经能够通过调用 acquireTokenPopup 来获取访问令牌

const result: AuthResponse = await this.msalService.acquireTokenSilent({
  scopes: [
    'user.read',
    "calendars.read",
    "calendars.read.shared",
    "calendars.readwrite",
    "group.read.all",
    "openid",
    "profile"
  ]
}).catch((error) => {
  if (error.name === "InteractionRequiredAuthError") {
    return this.msalService.acquireTokenPopup({
      scopes: [
        'user.read',
        "calendars.read",
        "calendars.read.shared",
        "calendars.readwrite",
        "group.read.all",
        "openid",
        "profile"
      ]
    })
  }
});

return result.accessToken;
Run Code Online (Sandbox Code Playgroud)

}

它可以工作,但不断弹出弹出窗口。有人能给我解释一下原因吗?

谢谢

Jie*_*Jie 0

我有类似的问题,只需使用以下代码来获取异常。

   this.broadcastService.subscribe('msal:acquireTokenFailure', (payload) => {
                    console.log(payload);    
                });
Run Code Online (Sandbox Code Playgroud)

InteractionRequiredAuthError:静默身份验证被拒绝。用户必须首先登录,并根据需要授予客户端应用程序对范围“api://1cfd57f1-ad48-4396-b95c-7fa782a178b6/access_as_user openid profile”的访问权限。

文件中写道:

InteractionRequiredAuthError:错误类,扩展了ServerError来表示服务器错误,需要交互式调用。如果用户需要与服务器交互以提供凭据或同意身份验证/授权,则 acquireTokenSilent 会引发此错误。错误代码包括“interaction_required”、“login_required”和“consent_required”

对于使用重定向方法(loginRedirect、acquireTokenRedirect)进行身份验证流程中的错误处理,您需要注册回调。

https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-handling-exceptions?tabs=javascript

所以我将“ this.msalService.loginRedirect() ”添加到回调方法中来解决我的问题。我也不太明白,为什么要再次授予权限,但是当人检查“MsalGuard”的实现时(https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/ msal-angular/src/msal-guard.service.ts),如果通过获取令牌引发异常,它会执行相同的代码“loginRedirect”。

顺便说一下,我还在我的 API“公开 API”中添加了客户端应用程序 ID,以授权客户端而无需征得同意。该异常最终在我的应用程序中得到修复。

在此输入图像描述