通过


在 iframe 中嵌入代码应用

本文介绍如何在外部主机中的 iframe 中嵌入代码应用,例如模型驱动应用 Web 资源、自定义网站或支持 iframe 的其他服务。 由于代码应用默认强制实施内容安全策略(CSP),因此必须更新 frame-ancestors 该指令以允许主机加载应用。

注释

  • 只有Power Apps同一租户中的用户才能访问嵌入的代码应用。 不能与租户外部的用户共享 iframe 的 URL。
  • 不能在任何本机桌面应用程序中嵌入代码应用,其中包括 Android 和 iOS。 此限制不包括第一方集成,例如 Teams 中的Power Apps。

先决条件

  • 必须是 Power Platform 环境的管理员才能更新 CSP 设置。
  • 代码应用必须部署到一个环境中。

获取代码应用程序的网址

若要嵌入代码应用,需要其播放链接。 URL 采用以下形式:

https://apps.powerapps.com/play/e/{environmentId}/app/{appId}?tenantid={tenantId}

可以通过在Power Apps中打开应用并从浏览器复制地址来查找完整的 URL。 tenantId 是一个可选查询参数,用于支持来宾访问,并确定从哪个租户打开应用。

将 iframe 添加到您的主机

获取代码应用的 URL 后,把<iframe> 元素添加到主机的 HTML 中。 将应用 URL src 替换为该值,并根据需要设置布局的宽度和高度。

<iframe
  width="1200"
  height="800"
  src="https://apps.powerapps.com/play/e/{environmentId}/{appId}"
  title="My code app">
</iframe>

注释

如果代码应用使用设备地理位置、麦克风、相机、全屏或剪贴板 API,请在 allow 属性中包含相应的权限:

<iframe ... allow="geolocation; microphone; camera; fullscreen; clipboard-write"></iframe>

配置内容安全策略(CSP)以允许框架

默认情况下,代码应用使用以下 frame-ancestors 指令,该指令限制哪些源可以在帧中加载应用:

frame-ancestors 'self' https://*.powerapps.com

如果在 https://*.powerapps.com 之外的任何主机(例如模型驱动应用、Dynamics 365实例或自定义网站)中嵌入代码应用,浏览器将阻止框架并记录类似于以下内容的 CSP 冲突:

Refused to frame 'https://<environment>.powerplatformusercontent.com/' because it violates the following Content Security Policy directive: "frame-ancestors 'self' https://*.powerapps.com"

若要解决此问题,请将主机源添加到 frame-ancestors 环境的指令。 例如:

  • 对于Dynamics 365或模型驱动应用:https://<your-org>.crm.dynamics.com
  • 对于自定义域: https://contoso.com

重要

自定义值与默认值 frame-ancestors 合并。 保存后,有效指令为:

frame-ancestors 'self' https://*.powerapps.com https://<your-org>.crm.dynamics.com

有关如何使用 Power Platform 管理中心或 REST API 更新 frame-ancestors 指令的说明,请参阅 “配置内容安全策略”。

配置内容安全策略
建筑