It turned out that I had to face integration with Facebook on the ASP.NET platform. The integration needed a server-side, so it was decided to use the Facebook C # SDK.
As it turned out, the process is not quite complicated, but it is important to understand the essence and principle.
Firstly a small disclaimer - the example below does not claim to be the proud title of production. His goal is to give a basic concept of the topic.
We divide the example into several steps:
Step 0: Minimum Theory
In fact, the minimum theory is bad. The theoretical part is important to understand, so as not to engage in copy-pasting without understanding what the code is doing.
Details of authentication / authorization from Facebook can (and should) be read here -
developers.facebook.com/docs/authenticationIn short, we are building a web application and we want:
- To authenticate users through Facebook.
- Get access to user data (name, userpic).
- To get the opportunity to post on the wall of the user on behalf of our application.
- A million dollars in small bills and a helicopter.
Next, we look at how to implement the first three points.
')
To work points 2 and 3, we need a certain
access token , which Facebook will issue only if item number one is successfully passed.
access token is a kind of confirmation of the access right that our application requested (and received) from the user.
To get this in our web application will be a LOGIN button. Clicking this button will open the standard Facebook authentication / authorization window. If necessary, the user will go through the standard Facebook enter username / password. Then he will be asked if he agrees to log in to the application XXX. The final step is to ask whether the user agrees to authorize the application XXX on his behalf to do actions A, B, C. (In the older version of the authentication dialog, the last two steps were on the same screen, now they are two different screens). After the user confirms all of the above, he will be redirected to the URL that we set at the very beginning of the process. As a parameter, Facebook will add to this URL some code (
code ), which we use to get the cherished
access token . It is important to remember that from the moment you press the LOGIN button, everything happens in a pop-up window. At the end of the authorization process, we will close it and reload the window with our web application.
So let's get started.
Step 1: Create a Facebook Application
Go to
developers.facebook.com/appsCreate a new app.

Specify the name of the application. App Namespace can be left blank. Carefully read and agree with the Facebook Platform Policies.

Remember (write) App ID and App Secret. We still need them.
Select the method of website integration. We enter
http: // localhost: 12345 / in the Site URL. authentication / authorization).

Everything. With the Facebook finished.
Step 2: Create and configure the project, add the Facebook SDK.
In Visual Studio, create a new project. For simplicity, I used the usual ASP.NET Web Application, with MVC everything is pretty identical. We call our project FBIntegrationSample.

Right click on References, select Add Library Package Reference ...

Go to the section Online - All, and look for facebook. We are interested in the three marked components. If you need to install all three for MVC applications, for the usual ASP.NET Web Application, the top two are enough.

Next little trick. With the default settings, IIS will use different ports for each restart, which does not suit us. Therefore, we go to the project properties, to the Web tab and set the specific port on which we want to start our server. For example - 12345.

The last step is to find an element called
<facebookSettings /> in the
web.config (usually at the very end of the file) and add the
appId and
appSecret obtained in the previous step.

Step 3: Code
Create a class
FBUser , which will store user data (for simplicity, after receiving user data we will save the object in the session)

Create a
Default.aspx , which will have a login button and two elements for user data.

In
Default.aspx.cx we will try to pull the
FBUser object
out of the session. If it didn't work out, create a link for the authorization window, otherwise use the user's data - we will show his name, userpic and even make a post on his wall.

Create the
FBAuthHandler.cs class. Using the
Facebook C # SDK, create a URL for the login window.

- redirectUrl - the URL to which the redirection will be made at the end of the authorization process.
- extendedPermissions is what will allow us to perform various actions on behalf of the user (to post on his wall, etc.).
- response_type should be code. Otherwise, Facebook will return us a token using the hash tag (#), i.e. from the server side, we will not have access to it, and this absolutely does not suit us.
- Display - popup. You can, of course, load the login window to the full screen instead of our application and then return to it, but this is not what we want.
More on extended permissionsMore about displayAdd a second method to
FBAuthHandler.cs , which will check if the response from
Facebook is returned with the parameter we need.

In case of success, we create the FBUser
object and put it into the session.
Add a little JavaScript in
Default.aspx , which when you click on the login button will open a pop-up window with the URL we need.

The last thing left for us is to create
FacebookOAuth.aspx , which will be redirected to after the end of the authorization process. It is important to remember that
FacebookOAuth.aspx will open in a pop-up window, so we use this file to
a. Check whether the code is returned from Facebook. If yes - we create the
FBUser object.

b. Close the pop-up window and reload the main window.

Step 4: Test





Source code available here -
http://dl.dropbox.com/u/5249810/habrapost01/FBIntegrationSample.zip