我正在尝试使用 MVC5 将谷歌地图加载到 iframe 中,但我被错误阻止了
Refused to display 'https://www.google.com/maps?cid=XXXXX' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
经过多次搜索,我尝试了以下方法:
将
AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
添加到 global.ascx 中的Application_Start
创建属性(已尝试使用或不使用 global.ascx 中的设置):
public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext != null) { filterContext.HttpContext.Response.Headers["X-Frame-Options"] = "ALLOW-FROM https://www.google.com"; base.OnActionExecuted(filterContext); } }
尝试使用属性
OnResultExecuted(ResultExecutedContext filterContext)
而不是OnActionExecuted
在 web.config 中删除它:
<httpProtocol> <customHeaders> <remove name="X-Frame-Options" /> </customHeaders> </httpProtocol>
我有什么遗漏的吗?我如何摆脱这个http header (或者至少更改它以允许 map )?
更新
我刚刚检查了发送的 header ,它们是正确的,因为它们要么说
X-Frame-Options: ALLOW-FROM https://www.google.com
或者如果我删除该属性但保留 global.ascx 更新,则根本不存在
但是,当我运行页面并看到这些 header 时,它仍然给出 SAMEORIGIN 错误。
最佳答案
事实证明我完全愚蠢并且误解了 x-frame-options 的工作原理。它是为了阻止您的网站页面通过 iframe 在另一个网站上显示。
所以我为 SAMEORIGIN 获取的 x-frame-options http header 实际上来自 google。我认为由于 url 是从他们的地方 api 返回的,所以我可以使用它,但显然你只能链接到它。
创建一个新的 map api key 并启用 map 嵌入 api,我可以改用 place_id 并将以下网址调用到 iframe 中:
https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY&q=place_id:PLACE_ID
这将在我没有获取标题(或对标题执行任何额外操作)的情况下显示。
我会把这个留在这里,以防万一有人像我一样愚蠢
关于c# - 使用 MVC5 允许来自不同域的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226355/