AADSTS9002326:仅允许“单页应用程序”客户端类型进行跨域令牌兑换。请求来源:'capacitor://localhost'

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

我有一个 Ionic 应用程序需要在 Azure 中进行身份验证,并且我遵循以下 stackoverflow: Ionic 和 MSAL 身份验证

一切都很顺利,除了iOS,我得到了

AADSTS9002326:仅允许“单页应用程序”客户端类型进行跨域令牌兑换。请求来源:'capacitor://localhost'

我确实尝试在手机和计算机网址上注册此网址,因为 SPA 需要有 http 或 https,所以这是无效的...

有人知道如何解决这个问题吗?

谢谢你

ios angular ionic-framework azure-ad-b2c capacitor
1个回答
0
投票

如果 Microsoft Entra ID 应用程序未配置为 SPA 并且您正在使用 SPA 身份验证,通常会出现错误“跨源令牌兑换仅适用于‘单页应用程序’客户端类型”。

在您的情况下,由于您的重定向 URL 是

capacitor://localhost
,因此您无法将其配置为 SPA,因为 SPA 仅支持 http 或 https。请参阅此MsDoc

因此 要解决该错误, 您必须将应用程序配置为移动和桌面应用程序并使用您的自定义

capacitor://localhost
重定向 URL:

enter image description here

对于样本

let config = MSALPublicClientApplicationConfig(clientId: "your-client-id",
redirectUri: "your-customredirect-uri",
authority: authority)
do {
  let application = try MSALPublicClientApplication(configuration: config)
 } catch let error as NSError {
 }

否则,如果您想使用SPA身份验证,那么您需要使用其他重定向URL https或http并将应用程序配置为单页应用程序:

enter image description here

browser.on('loadstart').subscribe(event => {
  if (event.url.includes('code')) {
    browser.close();
    const domain = event.url.split('#')[0];
    const url = event.url.replace(domain, 'http://***');
    console.log('will redirect to:', url);
    window.location.href = url;
  }
});
  • 默认情况下,您无法将自定义重定向URL添加到SPA平台。

参考:

通过 MSAL 使用重定向 URI (iOS/macOS) - Microsoft 身份平台 |微软

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