Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Using Facebook login in ASP.NET application without any third party library.

, 31 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 ste

Editorial Note

This article appears in the Third Party Product Reviews section. Articles in this section are for the members only and must not be used by tool vendors to promote or advertise products in any way, shape or form. Please report any spam or advertising.

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 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:

  1. Open and login with your Facebook credentials after logging in you will get a screen like this:

  1. Click on "Build for Websites" link, you will reach to , just click on 'Apps' menu Items (the last menu Item from right side), you will reach to Click on button, you will get following screen.

  2. 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).

  3. 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:

  1. Open Visual Studio.
  2. Select File --> New --> Project/Solution --> 'ASP.NET Empty Application'
  3. 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).

  4. Most of the code we will be doing on the HTML code of ASP.NET Page.
  5. For using Facebook login, we will have to use Facebook JavaScript SDK. There are three ways of using it:
    1. Load the JavaScript SDK asynchronously.
                // 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'); = id; js.async = true;
                    js.src = "//";
                    ref.parentNode.insertBefore(js, ref);
                } (document));
    2. Load the JavaScript SDK synchronously
      <script src= type="text/javascript"></script>
    3. 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>
  6. We will also require jquery for initializing the library. so copy following code in header section of the page.
    type="text/javascript"></script> <script
    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
                    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.

  7. Also copy the following code after Above code in to get the response back and fill the values.
            // This method will be called after the user login into facebook.
            function OnLogin(response) {
                if (response.authResponse) {
                    FB.api('/me?fields=id,name,gender,email,birthday', LoadValues);                    
            //This method will load the values to the labels
            function LoadValues (me) {
                if ( {
                    document.getElementById('displayname').innerHTML =;
                    document.getElementById('FBId').innerHTML =;
                    document.getElementById('DisplayEmail').innerHTML =;
                    document.getElementById('Gender').innerHTML = me.gender;
                    document.getElementById('DOB').innerHTML = me.birthday;
                    document.getElementById('auth-loggedin').style.display = 'block';
  8. Copy following code in the Body tag of the Page.
        <div id="fb-root"></div> <!-- This initializes the FB controls-->   
        <div class="fb-login-button" autologoutlink="true" scope="user_birthday,email" >
          Login with Facebook
         </div> <!-- FB Login Button -->   
        <!-- Details --> 
        <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/>        
  9. 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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

S Ravi Kumar-2012
India India
No Biography provided
Follow on   Google+

Comments and Discussions

QuestionNot working Pinmemberbobbybunty9-May-14 5:31 
QuestionObject doesn't support property or method '_gcb2' Pinmemberbishu_Visu7-Jan-14 1:48 
AnswerRe: Object doesn't support property or method '_gcb2' PinmemberS Ravi Kumar-20127-Jan-14 7:02 
GeneralRe: Object doesn't support property or method '_gcb2' Pinmemberbishu_Visu7-Jan-14 22:52 
GeneralRe: Object doesn't support property or method '_gcb2' Pinmemberbishu_Visu8-Jan-14 21:14 
Questionthanks PinmemberMember 97211334-Jan-14 1:24 
QuestionHow can i solve this error PingroupKM Perumal10-Nov-13 20:40 
AnswerRe: How can i solve this error PinmemberS Ravi Kumar-201210-Nov-13 21:34 
QuestionUser information doesn't display PinmemberMember 810335113-Jul-13 6:02 
QuestionPage_Load retrieve user id PinmemberMember 921312624-Jun-13 7:41 
QuestionRedirect to other page after Login PinmemberVenkatesh Yeluri11-Jun-13 16:52 
QuestionMy vote of 5 [modified] PinmemberKevinJeremy15-May-13 2:41 
Questionget city and country Pinmembershanalikhan10-Mar-13 12:43 
AnswerRe: get city and country PinmemberS Ravi Kumar-201211-Mar-13 3:06 
Questionunable to see information Pinmembershanalikhan10-Mar-13 3:04 
AnswerRe: unable to see information PinmemberS Ravi Kumar-201210-Mar-13 7:17 
GeneralRe: unable to see information Pinmembershanalikhan10-Mar-13 9:26 
GeneralRe: unable to see information PinmemberS Ravi Kumar-201210-Mar-13 9:31 
GeneralRe: unable to see information Pinmembershanalikhan10-Mar-13 9:40 
QuestionAwful tutorial and misleading title Pinmemberd-signet228-Feb-13 10:35 
GeneralRe: Awful tutorial and misleading title Pinmembermafnx14-Mar-13 6:24 
QuestionCan I do the same concept for my Windows Application? PinmemberGaneshcse13-Feb-13 8:03 
QuestionIs there any security threat for my application PinmemberSumanBiswas24-Jan-13 11:23 
AnswerRe: Is there any security threat for my application PinmemberS Ravi Kumar-201211-Jan-15 19:45 
Suggestionadditional help Pinmemberroger_276-Jan-13 7:24 
GeneralRe: additional help PinmemberGianluca SamBuca Di Pietro5-Feb-13 9:56 
GeneralRe: additional help Pinmemberroger_276-Feb-13 4:28 
GeneralRe: additional help PinmemberGianluca SamBuca Di Pietro8-Feb-13 1:03 
GeneralRe: additional help Pinmemberroger_2710-Feb-13 12:58 
GeneralRe: additional help PinmemberTom Psillas21-Oct-14 17:55 
GeneralMy vote of 5 PinmemberRafael Nicoletti20-Dec-12 7:18 
Questionlogin with facebook problem Pinmemberhasanbudak24-Nov-12 7:57 
AnswerRe: login with facebook problem PinmemberS Ravi Kumar-20123-Jan-13 20:55 
QuestionFb.Event.Subscribe Pinmembersafiyullah4-Sep-12 21:48 
AnswerRe: Fb.Event.Subscribe PinmemberS Ravi Kumar-20125-Sep-12 3:29 
The primary reason for Fb.Event.Subscribe doesn't work is that your FB Javascript Library is not being loaded, you will have to directly download the JS SDK from facebook site on your local machine and then reference it from there or if you are working in a secured environment the use the https link.
Let me know if you still face any issues.
S Ravi Kumar
GeneralRe: Fb.Event.Subscribe Pinmembersafiyullah5-Sep-12 19:39 
GeneralRe: Fb.Event.Subscribe PinmemberS Ravi Kumar-20126-Sep-12 16:43 
GeneralRe: Fb.Event.Subscribe Pinmembersafiyullah6-Sep-12 19:46 
GeneralRe: Fb.Event.Subscribe Pinmembersafiyullah17-Sep-12 19:41 
GeneralRe: Fb.Event.Subscribe PinmemberS Ravi Kumar-201219-Sep-12 20:08 
GeneralRe: Fb.Event.Subscribe Pinmembersafiyullah20-Sep-12 4:44 
GeneralRe: Fb.Event.Subscribe PinmemberS Ravi Kumar-201215-Nov-12 0:20 
AnswerRe: Fb.Event.Subscribe PinmemberDanko Greiner23-Jan-13 23:33 
GeneralRe: Fb.Event.Subscribe PinmemberS Ravi Kumar-201210-Mar-13 9:20 
QuestionMy vote of 3 (almost 4) PinmemberRafael Nicoletti2-Sep-12 16:28 
AnswerRe: My vote of 3 (almost 4) PinmemberS Ravi Kumar-20122-Sep-12 19:20 
GeneralMy vote of 4 PinmemberPrabhakaran Soundarapandian31-Aug-12 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 31 Aug 2012
Article Copyright 2012 by S Ravi Kumar-2012
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid