Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
31 Aug 2012 1  
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 http://developers.facebook.com 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 http://developers.facebook.com and login with your Facebook credentials. After logging in, you will get a screen like this:

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

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

  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.

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

  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:
       <script>
          // 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'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
              ref.parentNode.insertBefore(js, ref);
          } (document));
      </script>
    2. Load the JavaScript SDK synchronously:
      <script src=https://connect.facebook.net/en_US/all.js 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.
    <script
    src="scripts/jquery-1.8.0.min.js"
    type="text/javascript"></script>

    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 (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';
                        }
    }
    
  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/>
    </div>
    </div>
    
  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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here