You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Teams自定义应用认证成功后无法正常重定向求助

解决iOS端Teams自定义应用OAuth认证后重定向异常问题

我们开发了一款基于Microsoft Teams的自定义应用,采用外部认证提供商实现认证功能,已按照官方文档完成机制部署。该认证在桌面端Teams、Web端Teams及Android端Teams中均运行正常,用户完成认证后可正常重定向回自定义应用。

现有实现代码

//If using web teams app 
if (method === "page") {
            
      app.initialize().then(() => {
        app.getContext().then(async (context) => {
          authentication.notifySuccess(result!);
        })
      });
} 
// If using desktop, Android or iOS teams app
else if (method === "deeplink") {
     
      var encodedWebUrl = encodeURIComponent(`${window.location.origin}`);
      var encodedContext = encodeURIComponent(JSON.stringify({"subEntityId": result}));
      var deepLinkUrl = `https://teams.microsoft.com/l/entity/${authId}/dashboard?webUrl=${encodedWebUrl}&context=${encodedContext}`;
        
      window.location.href = deepLinkUrl;

      //window.location.href = `msteams://teams.microsoft.com/l/auth-callback?authId=${authId}&result=${result}`
} 
else {
    alert("failed");
}

问题描述

在iOS端Teams中使用该自定义应用时,用户完成外部认证后无法重定向至发起请求的自定义应用实例,而是在弹窗中打开应用。

复现步骤

  • 在iOS端Teams中打开自定义应用(登录页面)
  • 点击登录按钮,跳转至外部认证提供商的浏览器弹窗
  • 认证成功后,应用未返回主Teams界面的自定义应用实例,而是在弹窗中打开应用首页

解决方案

iOS端Teams对deeplink的处理逻辑与桌面/Android端存在差异,https协议的实体链接无法触发弹窗关闭并返回主应用实例,需针对iOS端单独使用msteams://协议的认证回调链接。

修改后的代码

//If using web teams app 
if (method === "page") {
    app.initialize().then(() => {
        app.getContext().then(async (context) => {
            authentication.notifySuccess(result!);
        })
    });
} 
// 处理桌面、Android端Teams
else if (method === "deeplink" && !isiOS()) {
    var encodedWebUrl = encodeURIComponent(`${window.location.origin}`);
    var encodedContext = encodeURIComponent(JSON.stringify({"subEntityId": result}));
    var deepLinkUrl = `https://teams.microsoft.com/l/entity/${authId}/dashboard?webUrl=${encodedWebUrl}&context=${encodedContext}`;
    window.location.href = deepLinkUrl;
}
// 单独处理iOS端Teams
else if (method === "deeplink" && isiOS()) {
    // 使用msteams协议的认证回调链接,自动关闭弹窗并返回原应用实例
    const encodedResult = encodeURIComponent(result);
    window.location.href = `msteams://teams.microsoft.com/l/auth-callback?authId=${authId}&result=${encodedResult}`;
} 
else {
    alert("failed");
}

// 辅助函数:检测当前设备是否为iOS
function isiOS() {
    return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
}

关键说明

  1. 协议选择:iOS端必须使用msteams://协议的auth-callback链接,这是Teams官方为认证场景设计的专属回调方式,能正确触发弹窗关闭并回到主应用实例。
  2. 参数编码:对result参数进行encodeURIComponent编码,避免特殊字符导致链接解析失败。
  3. 设备检测:通过用户代理字符串区分iOS设备,确保仅在iOS端启用专属逻辑。
  4. 版本兼容:确保iOS端Teams为最新版本,旧版本可能存在协议支持缺陷。

内容的提问来源于stack exchange,提问作者Anoop Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:32:29