我是 Facebook 应用程序的新手,最近阅读了可用的文档。然后,我继续从 NuGet 获取 Facebook C# SDK 并阅读入门教程。
我稍微修改了一下。我有 3 页:
- Default.aspx :具有“使用 facebook 登录”按钮
- Protected.aspx :我最终希望在没有先登录的情况下无法访问的页面
DoSignIn.aspx :包含对令牌的请求并重定向回 Default.aspx
在 Default.aspx 上,我有一个将我重定向到 Protected.aspx 页面的按钮。在它上面,我有一个标签显示我的令牌。
现在,我的问题是:
- 当我单击“使用 facebook 登录”按钮时,该按钮不会更改为显示我已登录。它仅在我刷新页面时显示我已登录。
- 如果我单击按钮将我重定向到 Protected.aspx 页面,我在标签中看不到令牌,因为从未调用过 DoSignIn.aspx 页面。
如果有人能指出我做错了什么(因为我几乎复制了教程示例),我将非常感激。在过去的 3 个小时里,我一直在摆弄这个并搜索谷歌和 SO,但不知道在哪里搜索了。
感谢您的时间。
PS。我有最新的 Facebook C# SDK 并且 App ID 似乎工作正常,因为当我手动刷新浏览器时,我确实已登录。
我添加了 3 页的代码:
默认.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div id="fb-root">
</div>
<script type="text/javascript">
window.fbAsyncInit = function () {
FB.init({
appId: 'I_INSERTED_MY_APP_ID_HERE', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
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;
// - 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", 'DoSignIn.aspx');
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>
<form id="form1" runat="server">
<div>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
Log in with Facebook</div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to protected" />
</div>
</form>
</body>
</html>
默认.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Protected.aspx");
}
}
DoSignIn.aspx.cs:
public partial class DoSignIn : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var accessToken = Request["accessToken"];
Session["AccessToken"] = accessToken;
Response.Redirect("Default.aspx");
}
}
受保护的.aspx.cs:
public partial class Protected : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["AccessToken"] != null)
{
Label1.Text = Session["AccessToken"].ToString();
}
else
{
Label1.Text = "Not authenticated";
}
}