我正在尝试使用facebook实现登录和注册系统。我正在使用以下代码:
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: 'MYAPPIDHERE', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location = "https://domain.com/fblogin.aspx";
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<fb:login-button autologoutlink="true" perms="email,offline_access" registration-url="https://domain.com/register_fb.aspx" />
我需要一些建议。目前,如果我登录Facebook,页面会自动触发'auth.login'事件并将我重定向到我的应用程序。它工作得很好,但我想防止这种行为。在一个理想的世界中,如果Facebook登录按钮只在点击它时触发'auth.login'事件,那就太好了。
你有什么想法?我应该废弃fb:login-button并创建一个与onclick事件相关联的按钮吗?如果有,你可以分享哪些好的例子?
由于
答案 0 :(得分:1)
我认为你应该保留你拥有的代码。给fb:login-button一个id(例如,id =“fb_loginbutton”)并包装你的...
window.location = "https://domain.com/fblogin.aspx";
...变成这样的......
var fb_loginbutton = document.getElementById('fb_loginbutton');
fb_loginbutton.onclick = function() {
window.location = "https://domain.com/fblogin.aspx";
}
(这在你的Event.subscribe ... auth.login函数里面。)
这样,成功的登录操作不会重定向浏览器,而只是在单击时告诉按钮重定向。
如果Facebook按钮没有说出正确的话,那么你可以改变它的HTML来说一些更有用的东西......
fb_loginbutton.innerHTML = "Click here to continue";
...或自己滚动。
我希望有所帮助!
答案 1 :(得分:0)
更新我设法找到了一个可行的解决方案:
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: 'MYAPPID', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
login();
});
FB.getLoginStatus(function(response) {
if (response.session) {
login();
}
});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function login() {
var fb_loginbutton = document.getElementById('fb_loginbutton');
$('.fb_button_text').html('Continue to your account...');
fb_loginbutton.onclick = function() {
window.location = "https://domain.com/login_controller.aspx";
}
}
</script>
我的login_controller.aspx页面如下所示:
受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load
Dim FacebookAPPID As String = "MY_FB_APP_ID"
If Request.Cookies("fbs_" & FaceBookAppID) Is Nothing Then
'Response.Redirect("error.aspx")
End If
Dim cookie As String = Request.Cookies("fbs_" & FaceBookAppID).Value
cookie = cookie.Replace("""", "")
Dim facebookValues As NameValueCollection = HttpUtility.ParseQueryString(cookie)
'Response.Write(facebookValues("access_token"))
'Response.Write("<br><br>")
'Response.Write(facebookValues("secret"))
'Response.Write("<br><br>")
'Response.Write(facebookValues("session_key"))
'Response.Write("<br><br>")
'Response.Write(facebookValues("sig"))
'Response.Write("<br><br>")
'Response.Write(facebookValues("uid"))
doLogin(facebookValues("uid").ToString)
End Sub
我有一个问题&gt;在我的数据库中存储某种AES加密字符串以及facebook用户ID是否有益?我正在使用此流程进行注册:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<fb:registration fields="[{'name':'name', 'view':'prefilled'},{'name':'username', 'view':'not_prefilled', 'description':'Username','type':'text'},{'name':'password', 'view':'not_prefilled', 'description':'Password','type':'text'},{'name':'lastname', 'description':'Last name','type':'text'},{'name':'email'},{'name':'accountnumber', 'description': ' Account number','type':'text'}]" onvalidate="validate_async" redirect-uri="https://domain.com/fbregister.aspx"></fb:registration>
<script>
function validate_async(form, cb) {
tmp = form.accountnumber;
tmp = tmp.split("-");
if (tmp.length != 2) {
cb({'accountnumber': 'Please include the dash in your account number. Example: xxxxx-xxxxxx.'});
}
if (form.username) {
if (form.username.length > 0) {
$.getJSON('checkUsernameExists.aspx?username=' + form.username,
function(response) {
if (response.error) {
cb({username: 'That username is taken'});
}
cb();
});
}
} else {
cb();
}
}
</script>
我的fbregister.aspx使用JSON.net库来解析facebook响应:
Dim FBAppID As String,FBSecret As String,data As JObject FBAppID =“MY_FB_ID” FBSecret =“MY_FB_SECRET”
data = ClientSite.Classes.Facebook.DecodeFacebookRequest(HttpContext.Current.Request.Form("signed_request"), FBSecret)
Response.Write(data)
使用密码存储userid的最佳做法是什么?我在考虑使用AES来加密密码。 userid是passphase,facebook secret是salt,系统生成的机器密钥是文本。
当有人尝试使用Facebook登录时,它会生成加密密码并将其与存储在数据库中的密码进行比较。这基本上提供了额外的安全层。想法?