Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

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

13 Nov 2010CPOL 22.7K  
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)