首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Facebook回调将'#_=_‘附加到返回的URL

Facebook回调将'#_=_‘附加到返回的URL
EN

Stack Overflow用户
提问于 2011-08-20 21:05:48
回答 22查看 123.9K关注 0票数 501

Facebook回调已开始将#_=_散列下划线附加到返回URL

有人知道为什么吗?解决方案是什么?

EN

回答 22

Stack Overflow用户

发布于 2011-09-04 15:14:17

通过Facebook's Platform Updates

会话重定向行为中的

更改

本周,当此字段保留为空时,我们开始向redirect_uri中添加片段#____=____。请确保您的应用程序可以处理此行为。

为了防止这种情况,在你的登录url请求中设置redirect_uri如下:(使用Facebook php-sdk)

代码语言:javascript
复制
$facebook->getLoginUrl(array('redirect_uri' => $_SERVER['SCRIPT_URI'],'scope' => 'user_about_me'));

更新

以上就是documentation所说的解决这个问题的方法。然而,Facebook记录的解决方案并不起作用。请考虑在Facebook Platform Updates blog post上发表评论,并关注this bug以获得更好的答案。在此之前,请在head标签中添加以下内容以解决此问题:

代码语言:javascript
复制
<script type="text/javascript">
    if (window.location.hash && window.location.hash == '#_=_') {
        window.location.hash = '';
    }
</script>

或者一个更详细的替代方案(感谢niftylettuce):

代码语言:javascript
复制
<script type="text/javascript">
    if (window.location.hash && window.location.hash == '#_=_') {
        if (window.history && history.pushState) {
            window.history.pushState("", document.title, window.location.pathname);
        } else {
            // Prevent scrolling by storing the page's current scroll offset
            var scroll = {
                top: document.body.scrollTop,
                left: document.body.scrollLeft
            };
            window.location.hash = '';
            // Restore the scroll offset, should be flicker free
            document.body.scrollTop = scroll.top;
            document.body.scrollLeft = scroll.left;
        }
    }
</script>
票数 243
EN

Stack Overflow用户

发布于 2013-08-19 08:47:07

TL;DR

代码语言:javascript
复制
if (window.location.hash === "#_=_"){
    history.replaceState 
        ? history.replaceState(null, null, window.location.href.split("#")[0])
        : window.location.hash = "";
}

包含分步说明的完整版

代码语言:javascript
复制
// Test for the ugliness.
if (window.location.hash === "#_=_"){

    // Check if the browser supports history.replaceState.
    if (history.replaceState) {

        // Keep the exact URL up to the hash.
        var cleanHref = window.location.href.split("#")[0];

        // Replace the URL in the address bar without messing with the back button.
        history.replaceState(null, null, cleanHref);

    } else {

        // Well, you're on an old browser, we can get rid of the _=_ but not the #.
        window.location.hash = "";

    }

}

一步一步:

当浏览器支持

  1. 方法时,如果fragment#_=_.
  2. Check,我们将只进入代码块。
    1. 通过在#上拆分并只取第一部分来清理URL。
    2. 告诉history用干净的URL替换当前页面状态。这将修改当前的历史记录条目,而不是创建新的历史条目。这意味着“后退”和“前进”按钮将以您想要的方式工作。;-)

  1. 如果浏览器不支持超棒的HTML5历史记录方法,那么只需通过将散列设置为空字符串来尽可能地清理URL。这是一个很糟糕的回退,因为它仍然会留下一个尾随的散列(example.com/#),而且它还会添加一个历史记录条目,因此back按钮将把您带回#_-_

了解有关history.replaceState的更多信息。

了解有关window.location的更多信息。

票数 122
EN

Stack Overflow用户

发布于 2017-01-29 11:25:35

这是Facebook出于安全原因而设计实现的。以下是Facebook团队成员埃里克·奥斯古德的解释:

这已被标记为“有意设计”,因为它可以防止潜在的安全漏洞。

一些浏览器会将来自URL的散列片段附加到它们被重定向到的新URL的末尾(如果该新URL本身没有散列片段)。

例如,如果example1.com返回到example2.com的重定向,则转到example1.com#abc的浏览器将转到example2.com#abc,example1.com中的散列片段内容将可由example2.com上的脚本访问。

由于可以将一个身份验证流重定向到另一个身份验证流,因此可以让来自一个应用程序的敏感身份验证数据可供另一个应用程序访问。

通过将新的散列片段附加到重定向URL以防止此浏览器行为,可以缓解此问题。

如果生成的URL的外观或客户端行为令人担忧,则可以使用window.location.hash (甚至是您自己的服务器端重定向)来删除违规字符。

来源:https://developers.facebook.com/bugs/318390728250352/

票数 61
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7131909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档