c# - Facebook 登录会永久重新加载页面

标签 c# javascript facebook asp.net-mvc-3 razor

这是我网页上的脚本:

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '419349611446911', // App ID
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

        // Additional initialization code here
        FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;
                // TODO: Handle the access token
                // Do a post to the server to finish the logon
                // This is a form post since we don't want to use AJAX
                var form = document.createElement("form");
                form.setAttribute("method", 'post');
                form.setAttribute("action", '@Url.Action("FacebookLogin", "Account")');

                var field = document.createElement("input");
                field.setAttribute("type", "hidden");
                field.setAttribute("name", 'accessToken');
                field.setAttribute("value", accessToken);
                form.appendChild(field);

                document.body.appendChild(form);
                form.submit();

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });
    };

    // Load the SDK Asynchronously
    (function (d) {
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    } (document));
</script>

这是我的:LogOnPartial:

@if(Request.IsAuthenticated) {
    <text>Hola, <strong>@User.Identity.Name</strong>!
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
    <div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1"></div>
}

当我更改为 auth.login 时,单击登录按钮不会登录,甚至不会重定向到任何地方。只是加载时的一小段闪光,没有其他任何事情。当我使用 auth.authReponseChange 时,我可以使用 Facebook 正确登录,但页面会一遍又一遍地重新加载。

我想问的是,如何修复这个错误?为什么auth.authResponseChange事件每次都会被触发?

示例:

我退出本地网站:

FormsAuthentication.SignOut();

即使这样,Facebook 按钮也会自动让我登录,然后循环继续。什么触发了登录按钮点击?

最佳答案

请尝试将事件订阅从 FB.Event.subscribe('auth.authResponseChange' 更改为 “授权.登录”。这只会在用户第一次登录时触发您的代码。

以下为from fb documentations

auth.login 当您的应用第一次注意到用户时(换句话说,在尚未获得有效 session 时获取 session ),会触发此事件。

auth.logout 当您的应用程序注意到不再有有效用户(换句话说,它有一个 session 但无法再验证当前用户)时,会触发此事件。

auth.authResponseChange 任何与身份验证相关的更改都会触发此事件,因为它们都会影响 session :登录、注销、 session 刷新。只要用户活跃于您的应用, session 就会随着时间的推移而刷新。

auth.statusChange 通常您会想要使用 auth.authResponseChange 事件。但在极少数情况下,您需要区分这三种状态:

  • 已连接
  • 已登录 Facebook,但未与您连接
  • 应用程序根本未登录 Facebook。

关于c# - Facebook 登录会永久重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701390/

相关文章:

python - 在 python 桌面应用程序中获取 facebook 用户/应用程序 token

reactjs - 命令 `brew install watchman` 运行 `chmod` 但最终安装失败

c# - 没有等待时删除信号量

javascript - 从服务器发送响应到网页

javascript - 将多个输入序列化为单个参数

javascript - 将 setTimeOut 绑定(bind)到具有特定元素的某个函数

android - Login with Facebook option trigger 建议下载一个应用程序

c# - JavaScript 和 C# 兼容的密码哈希

c# - CA1001 Visual Studio 2012 代码分析警告。这是什么意思?

c# - 如何比较两个列表并将值设置为列表属性