繁体   English   中英

即使用户已登录,如何强制用户重新输入Facebook密码

[英]How to force the user to re-enter facebook password even though he/she is logged in

我试图让用户在进入我网站的某些部分时重新输入密码。 我已经阅读到我应该使用“重新认证”授权类型来解决此问题,但是遇到两个问题。

  1. 当用户单击“重新认证”按钮时,密码已在“密码”输入框中填写。

  2. 当用户单击“重新认证”按钮,然后关闭或单击授权窗口上的取消时,我仍然会返回登录数据的响应,并且基本上可以“告诉”用户已登录的代码。

有没有人遇到过这些问题? 这是我的代码的一部分:

<fb:login-button id="facebook-reauth" onclick="return false;">Login with Facebook</fb:login-button>


<script type="text/javascript">
    $(document).ready(function () {
        document.getElementById('facebook-reauth').onclick = function () {
            FB.login(function (response) {
                if (response) {
                    window.location = '/Account/FacebookLogin?isCheckout=false';
                }
                else {
                    window.location = '/';
                }
            }, { scope: 'email', auth_type: 'reauthenticate' });
        };
    });
</script>

编辑:

我尝试将<fb:login-button />更改为<div />标记,但是它根本没有帮助。

非常感谢!

  1. 密码字段可能是预先填写的,因为您的浏览器只会为您记住密码,尝试在浏览器中清除密码,或者尝试使用浏览器无法记住的其他电子邮件。

  2. 我认为您只是忘了检查response.authResponse ,所以它应该是:

FB.login(function (response) {
    if (response.authResponse) {
        window.location = '/Account/FacebookLogin?isCheckout=false';
    }
    else {
        window.location = '/';
    }
}, { scope: 'email', auth_type: 'reauthenticate' });

编辑

是的,没错,即使用户取消了重新身份验证,也会执行回调,并且状态为“已连接”。
这可能是错误或其他问题,但是从我现在进行的测试来看,如果用户成功完成了重新身份验证过程,则令牌会发生更改,因此这应该可行:

var oldToken = FB.getAccessToken();

FB.login(function (response) {
    if (response.authResponse && response.authResponse.accessToken != oldToken) {
        window.location = '/Account/FacebookLogin?isCheckout=false';
    }
    else {
        window.location = '/';
    }
}, { scope: 'email', auth_type: 'reauthenticate' });

除了(或替代地)Nitzan的解决方案,还可以在后端添加验证。

如果你打电话:

https://graph.facebook.com/oauth/access_token_info?client_id=CLIENT_ID&access_token=ACCESS_TOKEN

您将收到以下响应:

“ access_token:...,

token_type:“轴承”,

expires_in:5937,

auth_type:“重新认证”

仅当用户通过授权模式成功重新验证后,auth_type才会设置为“ reauthenticate”。

还建议您验证expires_in> 0以确保令牌仍然有效。

if (response.status === 'connected') {
    /**
     * Even if the user cancels the re-authentication the callback is executed and the status is "connected".
     * This is probably a bug or something, but you can get info about the token and check that.    
     */
    FB.api('/debug_token', {
        input_token: response.authResponse.accessToken
    }, function(token_info) {

        if (typeof token_info.data.metadata != 'undefined') {

            if (token_info.data.metadata.auth_type == "reauthenticate") {

            }
        } else {
            //user did not re-authenticate so do nothing
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM