Click here to Skip to main content
15,878,814 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
13 Nov 2010CPOL 22K   5   3
Simple example to integrate FB javascript API to implement FB Connect application, fast, simple and trusted way to implement FB connect.
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
XML
<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)


Written By
Chief Technology Officer
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?? Pin
Alesha Mary Milred Fernandes26-Nov-13 23:07
Alesha Mary Milred Fernandes26-Nov-13 23:07 
Questionhow to get user info at code behind Pin
anto@almeka16-Mar-12 21:36
anto@almeka16-Mar-12 21:36 
GeneralReason for my vote of 5 efficient Pin
Monjurul Habib13-Nov-10 22:12
professionalMonjurul Habib13-Nov-10 22:12 

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.