Integrate FaceBook API with ASP.Net

Steps to Integrate FaceBook API with ASP.Net is explained below.

1)Create a Facebook application to get an AppID and Secret key for your Website or Blog. Use the following link to open Facebook developers App site.

https://developers.facebook.com/apps

2)Add App ID,secret key in code and also make reference to DLL for Facebook integration.

Download DLL for FaceBook Integration here

Aspx Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Facebook Login and Logout</h2>
            <asp:Button ID="btnLogin" runat="server" Text="Login with FaceBook" OnClick="Login" />
            <asp:Panel ID="pnlFaceBookUser" runat="server" Visible="false">
                <hr />
                <table>
                    <tr>
                        <td rowspan="5" valign="top">
                            <asp:Image ID="ProfileImage" runat="server" Width="50" Height="50" />
                        </td>
                    </tr>
                    <tr>
                        <td>ID:<asp:Label ID="lblId" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>Name:<asp:Label ID="lblName" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="Logout" />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
        </div>
    </form>
</body>
</html>

 

C# Code

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string API_Key = ConfigurationManager.AppSettings["API_Key"];
            string API_Secret = ConfigurationManager.AppSettings["API_Secret"];
            FaceBookConnect.API_Key = API_Key;
            FaceBookConnect.API_Secret = API_Secret;
            if (!IsPostBack)
            {
                if (Request.QueryString["logout"] == "true")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User has logged out from Facebook')", true);
                    return;
                }

                if (Request.QueryString["error"] == "access_denied")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User has denied access.')", true);
                    return;
                }

                string code = Request.QueryString["code"];
                if (!string.IsNullOrEmpty(code))
                {
                    string data = FaceBookConnect.Fetch(code, "me");
                    FaceBookUser faceBookUser = new JavaScriptSerializer().Deserialize<FaceBookUser>(data);
                    faceBookUser.PictureUrl = string.Format("https://graph.facebook.com/{0}/picture", faceBookUser.Id);
                    pnlFaceBookUser.Visible = true;
                    lblId.Text = faceBookUser.Id;
                    lblName.Text = faceBookUser.Name;
                    ProfileImage.ImageUrl = faceBookUser.PictureUrl;
                    btnLogin.Enabled = false;
                }
            }
        }
        protected void Login(object sender, EventArgs e)
        {
            FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split('?')[0]);
        }
        protected void Logout(object sender, EventArgs e)
        {
            FaceBookConnect.Logout(Request.QueryString["code"]);
        }
    }
    public class FaceBookUser
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string PictureUrl { get; set; }
    }

Output

The below demo shows how to get the details of logged In user using FaceBook API.

 

Demo Code

Download