Click here to Skip to main content
15,607,524 members
Articles / Web Development / HTML
Technical Blog
Posted 31 Aug 2012


43 bookmarked

Using Facebook Login in ASP.NET Application Without Any Third Party Library

Rate me:
Please Sign up or sign in to vote.
4.75/5 (18 votes)
31 Aug 2012CPOL3 min read
How to use Facebook login in ASP.NET application without any third party library

Facebook is extremely popular these days and you will hardly find anyone without a Facebook profile. More and more people now want their 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 a 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 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 your website in the form of an App in Facebook.

So let's 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:

    Image 1

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

    Image 3

  3. 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 the below screenshot (Just masked the AppID).

    Image 4

  4. Give the URL of your website/ application Site URL text box of the app summary screen, like the 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.

    Image 5

Now you are ready to create an 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.)

    Image 6

  4. We will be doing most of the code 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 the following code in the 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 the above code 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 the 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 then 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)

Written By
India India
Hi There, I am an IT professional with 14 years of experience in architecting, designing and building IT solutions for complex business needs in form of mobile & web applications using Microsoft technologies. Currently working in an multinational company in India as Solutions Architect. The articles here are sourced from my blog :

Comments and Discussions

Questionscreen shot images Pin
Member 1205052211-Oct-15 10:17
Member 1205052211-Oct-15 10:17 
QuestionNot working Pin
bobbybunty9-May-14 4:31
bobbybunty9-May-14 4:31 
QuestionObject doesn't support property or method '_gcb2' Pin
bishu_Visu7-Jan-14 0:48
bishu_Visu7-Jan-14 0:48 
AnswerRe: Object doesn't support property or method '_gcb2' Pin
S Ravi Kumar (TechieRathore)7-Jan-14 6:02
professionalS Ravi Kumar (TechieRathore)7-Jan-14 6:02 
GeneralRe: Object doesn't support property or method '_gcb2' Pin
bishu_Visu7-Jan-14 21:52
bishu_Visu7-Jan-14 21:52 
GeneralRe: Object doesn't support property or method '_gcb2' Pin
bishu_Visu8-Jan-14 20:14
bishu_Visu8-Jan-14 20:14 
Questionthanks Pin
Member 97211334-Jan-14 0:24
Member 97211334-Jan-14 0:24 
QuestionHow can i solve this error Pin
KM Perumal10-Nov-13 19:40
KM Perumal10-Nov-13 19:40 
AnswerRe: How can i solve this error Pin
S Ravi Kumar (TechieRathore)10-Nov-13 20:34
professionalS Ravi Kumar (TechieRathore)10-Nov-13 20:34 
QuestionUser information doesn't display Pin
Member 810335113-Jul-13 5:02
Member 810335113-Jul-13 5:02 
QuestionPage_Load retrieve user id Pin
InfoMidia Domenico Mancina24-Jun-13 6:41
InfoMidia Domenico Mancina24-Jun-13 6:41 
QuestionRedirect to other page after Login Pin
Venkatesh Yeluri11-Jun-13 15:52
Venkatesh Yeluri11-Jun-13 15:52 
QuestionMy vote of 5 Pin
KevinJeremy15-May-13 1:41
KevinJeremy15-May-13 1:41 
Questionget city and country Pin
shanalikhan10-Mar-13 11:43
shanalikhan10-Mar-13 11:43 
AnswerRe: get city and country Pin
S Ravi Kumar (TechieRathore)11-Mar-13 2:06
professionalS Ravi Kumar (TechieRathore)11-Mar-13 2:06 
Questionunable to see information Pin
shanalikhan10-Mar-13 2:04
shanalikhan10-Mar-13 2:04 
AnswerRe: unable to see information Pin
S Ravi Kumar (TechieRathore)10-Mar-13 6:17
professionalS Ravi Kumar (TechieRathore)10-Mar-13 6:17 
GeneralRe: unable to see information Pin
shanalikhan10-Mar-13 8:26
shanalikhan10-Mar-13 8:26 
GeneralRe: unable to see information Pin
S Ravi Kumar (TechieRathore)10-Mar-13 8:31
professionalS Ravi Kumar (TechieRathore)10-Mar-13 8:31 
GeneralRe: unable to see information Pin
shanalikhan10-Mar-13 8:40
shanalikhan10-Mar-13 8:40 
QuestionAwful tutorial and misleading title Pin
d-signet228-Feb-13 9:35
d-signet228-Feb-13 9:35 
GeneralRe: Awful tutorial and misleading title Pin
mafnx14-Mar-13 5:24
mafnx14-Mar-13 5:24 
QuestionCan I do the same concept for my Windows Application? Pin
Ganesh KP13-Feb-13 7:03
professionalGanesh KP13-Feb-13 7:03 
Hi Ravi Kumar I want the same concept like facebook authenitication in my application. Can it is possible?
QuestionIs there any security threat for my application Pin
SumanBiswas24-Jan-13 10:23
SumanBiswas24-Jan-13 10:23 
AnswerRe: Is there any security threat for my application Pin
S Ravi Kumar (TechieRathore)11-Jan-15 18:45
professionalS Ravi Kumar (TechieRathore)11-Jan-15 18:45 

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

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