Thursday, August 30, 2012

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

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:

2) 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.
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 below screenshot (Just masked the AppID).
4) 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:
        a) 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));


        b) Load the JavaScript SDK synchronously
         <script src= type="text/javascript"></script>

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

<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

                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.

8) 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';
9) 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/>       


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

Download Code From Here

Reference Links :


    Trekking in Nepal - Step into our world. It’s a world of lush green valleys, fertile terraced fields laden with fresh cultivated crops, poring into quiet pine forests of junipers, across glittering glacial lakes... & all this in the backdrops of towering silver grey peaks that speak of a world brimming with Everest base camp Trek. pure adventure... it’s a world that begins with ENCOUNTERS NEPAL.COM... & it never ends there... because it’s a wonderful world!!!
    Trekking in Nepal
    Everest base camp trekking
    Annapurna base camp trekking
    Manaslu Circuit Trekking
    Upper Mustang Trekking
    Annapurna Circuit Trekking
    Langtang Valley Trekking
    Kanchanjunga Trekking
    Upper dolpo trekking
    Tsum Valley Trekking
    Island Peak climbing
    Ghorepani Poonhill Trekking
    Mansarovar Kailash (Shiva Parbat) Yatra
    Gangajamuna Ganesh Parbat Trek
    Holiday Package in Nepal
    Mt flight in Nepal
    Expedition in Nepal
    Trekking in Bhutan

  2. I really Enjoy the reading on this post, please continue it.

    SEO Sydney | SEO Melbourne | SEO

  3. You have given very nice post it is very knowledgable and result oriented.

    Website is very good tool for any company. development company Delhi

  4. provides ASP.NET in hyderabad with best faculties on real time projects. We give the best online trainingamong the ASP.NET in Hyderabad. Classroom Training in Hyderabad India

  5. Hi,I am getting error 'Given URL is not permitted by the application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.'
    How to resolve this?

    1. Hi Sneha,

      The issue seems like your app is not created/registered in Facebook. I tried to send you the code sharing link but it gave me error that your email doesn't allows outside domain URLs.

      I have wrote this blog around one year back and haven't explored it further, so I would suggest you to follow the help links given above in article and go through the code.

      S Ravi Kumar

  6. trekking in Nepal organized by the trekking Guide own agency and 15 years experience Guide trekking agency Mountain Air Guided Adventures(p.)Ltd. Nepal is the trekking paradise in the world, here are varieties of the hiking and trekking trail.Expereince Guide and professional service

    Everest base camp trek poon hill trek

    Everest view trek

    Nepal short trek

    Dhaulagiri trek

    dolpo trek

    Rara lake trek

    manaslu trekking
    Best and latest tea house and classical camping route. However it is both suitable for camping and tea house trekking in Nepal.

    mustang trekking

  7. Join Our new batch for ASP .Dot Net Training in Jaipur. Contact us on

  8. Thanks for sharing this useful information..Its really very informative.

    Dot Net Training Chennai

  9. It was really a nice article and I was really impressed by reading this article. We are also giving all software Course Online Training. The ASP.Net Online Training is one of the leading Online Training institute in the world.

  10. More informative suggesion.Thanks a lot. The Keshri Software Solutions provides Web Application development,Website Promotions, Search Engine Optimizations services .we have a very dedicated and hard working team of web application developers( server/MVC) , Search engine optimizers. Fast communication and quality delivery product is our commitment.

    Get more information please visit to - .

  11. brillant piece of information, I had come to know about your web-page from my friend hardkik, chennai,i have read atleast 9 posts of yours by now, and let me tell you, your webpage gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanx a million once again, Regards, obiee training in hyderabad,informatica training in hyderabad

  12. This comment has been removed by the author.

  13. Wow!!! Honestly speaking you are really a great writer. What I required I got it. Thank you so much..NET Training

  14. I really enjoyed this posting in which you share a valuable post. Thanks for sharing it.
    Facebook app development companies

  15. Hi,
    Thanks for sharing the useful information about the topic and I found this as very informative which was very intresting to read for further information visit
    Oracle Fusion

  16. Hi,
    very nice article it contains some new information which i don't know worthy to read this type of articles.
    Thank you.
    oracle EBS training

  17. ERPTREE handles oracle fusion HCM training and its modules maintaining classroom based training with
    the self-paced videos. An expert having ten plus years of self-experience handles the training period through
    online and explains each and every point perfectly. We recently launched our institute in the USA and getting
    the best reputation over there.

    Oracle fusion HCM Online Training

    Oracle Fusion HCM Training

  18. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor
    lead live training in Oracle Fusion Financials online Training , World Class Virtual Instructor led training on Oracle fusion Financials Training. We have industry expert trainer. We provide Training Material and Software Support. calfre has successfully conducted 100000+ trainings in Hyderabad, Ameerpet, Mumbai, Chennai, India, USA, UK, Australlia,
    Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.

  19. Somebody necessarily help to make severely posts I might state. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Well done admin..
    Dot Net Training in Chennai