Facebook is extremely popular these days and you will hardly find anyone without Facebook profile, more and more people now want there site to use Facebook authentication in their application, instead of using their own user database to maintain user credentials. In this article I will give you step by step demo on how to integrate Facebook login into you screen.
Step-1: Register site in Facebook:
In order to use Facebook login, you should must have a verified developer account in Facebook (which is very easy to create just login to http://developers.facebook.com using your Facebook credentials and follow the screen instructions)
and you must register you website in the form of an App in Facebook.
So lets start with registering your site with Facebook:
- Open http://developers.facebook.com and login with your Facebook credentials after logging in you will get a screen like this:

- Click on "Build for Websites" link, you will reach to https://developers.facebook.com/docs/guides/web/ , just click on 'Apps' menu Items (the last menu Item from right side), you will reach to https://developers.facebook.com/apps. Click on
button, you will get following screen.

- Enter the name of your website in place of App Name, rest of the fields are optional so fill them if you require to use them and click on 'Continue' button. you will be asked to fill a captcha screen and then you will get the summary screen like below screenshot (Just masked the AppID).

- Give the URL of you website/ application Site URL text box of the app summery screen, like below screenshot. I have given localhost address as I will be testing the app from my local build, you will have to give your website URL in this.
Now you are ready to create ASP.Net Web Application, to use Facebook integration.
Step-2: Create ASP.NET application using Facebook login:
- Open Visual Studio.
- Select File --> New --> Project/Solution --> 'ASP.NET Empty Application'
- Now Right Click on Project Name Select Add--> New Item from the pop-up menu and click on 'Web Form' to add Default.Aspx page. (See screenshot).

- Most of the code we will be doing on the HTML code of ASP.NET Page.
- For using Facebook login, we will have to use Facebook JavaScript SDK. There are three ways of using it:
- Load the JavaScript SDK asynchronously.
<script>
// Load the SDK Asynchronously
(function (d) {
var js, id = if (d.getElementById(id)) { return; }
js = d.createElement( js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
ref.parentNode.insertBefore(js, ref);
} (document));
</script>
- Load the JavaScript SDK synchronously
<script src=https://connect.facebook.net/en_US/all.js type="text/javascript"></script>
- Download the SDK JavaScript file in your local project folder and use it from there. Implemented in 'Default.aspx' of solution.
<script src="scripts/all.js" type="text/javascript"></script>
- We will also require jquery for initializing the library. so copy following code in header section of the page.
<script
src="scripts/jquery-1.8.0.min.js"
type="text/javascript"></script> <script
src="scripts/all.js"
type="text/javascript"></script> 7) Now to initialize the Facebook SDK copy following code in a new script section :
$("document").ready(function () {
// Initialize the SDK upon load
FB.init({
appId: 'YOUR_APP_ID', // App ID
channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
scope: 'id,name,gender,user_birthday,email', // This to get the user details back from Facebook
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', OnLogin);
});
Here YOUR_APP_ID will be the App Id you will get from the Facebook App.
- Also copy the following code after Above code in to get the response back and fill the values.
function OnLogin(response) {
if (response.authResponse) {
FB.api('/me?fields=id,name,gender,email,birthday', LoadValues);
}
}
function LoadValues (me) {
if (me.name) {
document.getElementById('displayname').innerHTML = me.name;
document.getElementById('FBId').innerHTML = me.id;
document.getElementById('DisplayEmail').innerHTML = me.email;
document.getElementById('Gender').innerHTML = me.gender;
document.getElementById('DOB').innerHTML = me.birthday;
document.getElementById('auth-loggedin').style.display = 'block';
}
}
- Copy following code in the Body tag of the Page.
<div id="fb-root"></div> <!---->
<div class="fb-login-button" autologoutlink="true" scope="user_birthday,email" >
Login with Facebook
</div> <!---->
<!---->
<div id="auth-status">
<div id="auth-loggedin" style="display: none">
Hi, <span id="displayname"></span><br/>
Your Facebook ID : <span id="FBId"></span><br/>
Your Email : <span id="DisplayEmail"></span><br/>
Your Sex:, <span id="Gender"></span><br/>
Your Date of Birth :, <span id="DOB"></span><br/>
</div>
</div>
- Your application is ready now, execute the application.
In this blog we have learned how to register an application in Facebook and the use that application for Facebook authentication in our ASP.NET website/application. Using this the user of your web site/application will be free from the hassle of registering and entering their details again if they have Facebook Id they can use that to login.
Reference Links