I followed this excellent guide on how to use Facebook login. I modified it a little to retrieve the
response.authResponse.accessToken
So, right after this:
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
I added this line:
var accessToken = response.authResponse.accessToken;
Now, I can’t find a way to pass this javascript accessToken variable to my server-side C# code.
What I would like is that in the Page_Load() event, it checks if an accessToken is present. If yes, it would not display the “Login with Facebook” div and so I can proceed to use the Access Token from my server-side code.
If the user is not currently logged in, I would like him to log with Facebook and then have my page server-side code to receive the Access Token.
Anybody knows how to do this?
Thank you for your help, I’m losing my mind over this.
PS. I don’t want to redirect the user to another page once he logs in. I just want the page he’s on to know that he is logged in and proceed from here with server-side code.
In my opinion, the best approach from implementing Facebook Login is to carry out all functionality server side. Using a REST library such as Hammock, you can achieve this.
Here’s a really useful article on how to implement: http://www2.suddenelfilio.net/2010/09/08/connect-to-facebook-using-asp-net-facebook-graph-api-hammock/
In addition to that link, here is some code that will point you in the right direction:
Login Page
Add an ASP.NET button to this login page with the following click event:
Callback Page
As you have seen in the button click event, the
redirect_uriis going to point to our callback page. This callback page will carry out all the functionality to get the access token and any other login jobs you need to carry out:Note: This will require the use of the Hammock REST library as pointed out at the beginning of my post.
I hope this helps.