Click here to Skip to main content
Click here to Skip to main content

Integrate FB javascript API to your asp.net app to implement FB connect

By , 13 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Hi,
 
I am going to demonstrate how you can use FB Javascript API to implement Facebook connect applications, as the facebook toolkit for .net is extremely unstable. You have to integrate the below code in your app.
 
Prerequisite


1)You need a channel.html file , content of that file is only this tag nothing else not even any standard html tags
<script src="http://connect.facebook.net/en_US/all.js"></script>
2) Top of the page where you are using this code replace your html tag by this tag (required for consistency across browsers)
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
 
Code
<div>
    <fb:login-button perms="email,user_photos" size="large" length="long">Login with Facebook</fb:login-button>
<div style="display: none;">
    <asp:Button ID="fbRegister" runat="server" Text="Ok"
        onclick="fbRegister_Click"  />
</div>
<input id="hidEmail" type="hidden" runat="server" />
<input id="hidFirstName" type="hidden" runat="server" />
<input id="hidLastName" type="hidden" runat="server" />
<input id="hidUID" type="hidden" runat="server" />
<div id="fb-root">
</div>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
    FB.init({ appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true, channelUrl: "channel.html" });
 
    FB.Event.subscribe('auth.sessionChange', function(response) {
        if (response.session) {
            FB.api('/me', function(response) {
                $("#<%=hidEmail.ClientID %>").val(response.email);
                $("#<%=hidFirstName.ClientID %>").val(response.first_name);
                $("#<%=hidLastName.ClientID %>").val(response.last_name);
                $("#<%=hidUID.ClientID %>").val(response.id);
                $("#<%= fbRegister.ClientID %>").click();
            });
 
        } else {
            // The user has logged out, and the cookie has been cleared
        }
    });
function LoginWithFB() {
        $("#<%= fbRegister.ClientID %>").click();
    }
</script>
    </div>


Replace YOUR_APP_ID with your application id that you get during application registration with facebook. You can download sample code from here[^]

License

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

About the Author

Shahriar Iqbal Chowdhury/Galib
Technical Lead
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here
 
Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
QuestionHow to get the user information back?? PinmemberAlesha Mary Milred Fernandes26-Nov-13 23:07 
Questionhow to get user info at code behind Pinmemberanto@almeka16-Mar-12 21:36 
GeneralReason for my vote of 5 efficient PinmemberMonjurul Habib13-Nov-10 22:12 
Reason for my vote of 5
efficient

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 14 Nov 2010
Article Copyright 2010 by Shahriar Iqbal Chowdhury/Galib
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid