Reputation: 993
I'm new to the Facebook apps and recently read the doc available. Then, I proceeded to get the Facebook C# SDK from NuGet and read the get started tutorial.
I modified it a little. I have 3 pages:
DoSignIn.aspx : contains the request for the token and redirects back to Default.aspx
On the Default.aspx, I have a button that redirects me to the Protected.aspx page. On it, I have a label showing me the token.
Now, my issue is this:
If someone could point me in what I do wrong (as I nearly copied the tutorial example), I would be very grateful. I spent the past 3 hours fiddling with this and searching Google and SO all over and don't know where to search anymore.
Thank you for your time.
PS. I have the latest Facebook C# SDK and the App ID seems to work fine as when I manually refresh my browser, I am indeed logged in.
I add the code for the 3 pages:
Default.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>
Default.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");
}
}
Protected.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";
}
}
Upvotes: 1
Views: 3090
Reputation: 993
Right, I have spent hours searching and finally found why it is not working. It has nothing to do with the code above but with a Facebook bug. Actually, it seems that many people can't have the auth.authResponseChange to fire as the session refreshes. I found several bugs reports on FB describing the complete issue, one of them being here.
This issue I had when developing on /localhost/. I had already moved to a different way to integrate Facebook Login when, before deleting the above samples, I tried uploading it on an actual hosting server under a domain name. For a reason I can't explain, it just works from there.
Anyway, the implementation for the Facebook login I'm currently pursuing is explained here.
Upvotes: 1