我正尝试按照以下指南在我的应用程序中实现 Facebook 登录:https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.0
Facebook 的东西一切正常:)
但是在 facebook 身份验证完成后,我想将所有适当的变量传递到网页后面的 C# 代码以进入我的数据库,然后再重定向到注册过程的下一部分。
我知道如何在 HTML 中使用 asp 标签来做到这一点,例如:
<asp:ImageButton id=ctrlNext runat="server" ImageUrl="Btn_Next.gif" onclick="ctrlNext_Click"></asp:ImageButton>
但是,如果从 Facebook 完成后控件当前在 javascript 函数中 - 我不希望用户必须手动单击按钮才能继续 - 理想情况下,他们单击 facebook 登录按钮,我调用 facebook API,然后调用页面重定向已存储的信息。
请您参考如下方法:
确保包含以下 using 语句:
using System.Web.Services;
你的 PageMethod 应该是这样的:
[WebMethod]
public static string fbLogin()
{
/* Do your DB Magic Here */
}
使用 jQuery.ajax 方法:
FB.login(function(response) {
if (response.status === 'connected') {
// Logged into your app and Facebook.
$.ajax({
type: "POST",
url: "YouPageName.aspx/fbLogin",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
//Like a redirect
document.location.href = "http://www.somewherecool.com/newPage.aspx";
}
});
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
});
这只是让您入门的基本框架。不要忘记充实错误处理等。
然而
当您要执行重定向时,您有点违背了使用 AJAX 的目的。有几种方法可以使用 javascript 获取要回发的页面。最快、最简单和最 hackiest 的方法是使用一个带有通过 CSS 隐藏的普通服务器端单击处理程序的 asp 按钮。然后在下面的例子中使用javascipt(jquery)来点击按钮。
按钮
<asp:ImageButton id="ctrlNext" style="display:none" runat="server" ImageUrl="Btn_Next.gif" onclick="ctrlNext_Click"></asp:ImageButton>
javascript
FB.login(function(response) {
if (response.status === 'connected') {
// Logged into your app and Facebook.
//CLick the button
$("#<%=ctrlNext.ClientID%>").click();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
});
