Click here to Skip to main content
12,509,640 members (55,363 online)
Click here to Skip to main content
Add your own
alternative version

Stats

147.3K views
38 bookmarked
Posted

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.

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:

  1. Open http://developers.facebook.com 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 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.

  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.
       <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 following code in 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 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 (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 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 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

License

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

Share

About the Author

S Ravi-Kumar-2012
Architect
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 : http://err2solution.com/

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: additional help Pin
roger_2710-Feb-13 11:58
memberroger_2710-Feb-13 11:58 
GeneralRe: additional help Pin
Tom Psillas21-Oct-14 16:55
memberTom Psillas21-Oct-14 16:55 
GeneralMy vote of 5 Pin
Rafael Nicoletti20-Dec-12 6:18
memberRafael Nicoletti20-Dec-12 6:18 
Questionlogin with facebook problem Pin
hasanbudak24-Nov-12 6:57
memberhasanbudak24-Nov-12 6:57 
AnswerRe: login with facebook problem Pin
S Ravi Kumar-20123-Jan-13 19:55
memberS Ravi Kumar-20123-Jan-13 19:55 
QuestionFb.Event.Subscribe Pin
safiyullah4-Sep-12 20:48
membersafiyullah4-Sep-12 20:48 
AnswerRe: Fb.Event.Subscribe Pin
S Ravi Kumar-20125-Sep-12 2:29
memberS Ravi Kumar-20125-Sep-12 2:29 
GeneralRe: Fb.Event.Subscribe Pin
safiyullah5-Sep-12 18:39
membersafiyullah5-Sep-12 18:39 
Thanks Ravikumar..

FB javascript library loaded correctly.. FB.Event.Subscribe('auth.login') doesn't work.. past one week i am facing this issue... Yesterday only i find the solution. The exact reason is I never set Site url into my facebook application settings... I was read lot of articles regarding this issue. But no body tells this point.. If we set our domain name into the site url field then all the events working perfectly..

Thanks & Regards
Safiyullah
GeneralRe: Fb.Event.Subscribe Pin
S Ravi Kumar-20126-Sep-12 15:43
memberS Ravi Kumar-20126-Sep-12 15:43 
GeneralRe: Fb.Event.Subscribe Pin
safiyullah6-Sep-12 18:46
membersafiyullah6-Sep-12 18:46 
GeneralRe: Fb.Event.Subscribe Pin
safiyullah17-Sep-12 18:41
membersafiyullah17-Sep-12 18:41 
GeneralRe: Fb.Event.Subscribe Pin
S Ravi Kumar-201219-Sep-12 19:08
memberS Ravi Kumar-201219-Sep-12 19:08 
GeneralRe: Fb.Event.Subscribe Pin
safiyullah20-Sep-12 3:44
membersafiyullah20-Sep-12 3:44 
GeneralRe: Fb.Event.Subscribe Pin
S Ravi Kumar-201214-Nov-12 23:20
memberS Ravi Kumar-201214-Nov-12 23:20 
AnswerRe: Fb.Event.Subscribe Pin
Danko Greiner23-Jan-13 22:33
memberDanko Greiner23-Jan-13 22:33 
GeneralRe: Fb.Event.Subscribe Pin
S Ravi Kumar-201210-Mar-13 8:20
memberS Ravi Kumar-201210-Mar-13 8:20 
QuestionMy vote of 3 (almost 4) Pin
Rafael Nicoletti2-Sep-12 15:28
memberRafael Nicoletti2-Sep-12 15:28 
AnswerRe: My vote of 3 (almost 4) Pin
S Ravi Kumar-20122-Sep-12 18:20
memberS Ravi Kumar-20122-Sep-12 18:20 
GeneralMy vote of 4 Pin
Prabhakaran Soundarapandian31-Aug-12 6:53
memberPrabhakaran Soundarapandian31-Aug-12 6:53 

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.

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