Facebook Login Button for ASP.NET

Facebook Login Button control is used to connect a Facebook Connect web site with the Facebook and to share data, using Facebook API. It also enables that, once user is logged in, all controls from the library will work without the requirement for logging. The control has an optional event handler which is executed after user is logged in. By using this event handler, it gives you a possibility to execute additional code after user logs in, for example, to redirect to registration page for new users or user account page for existing users. The control is available in C# and VB.NET programming languages, as part of Facebook ASP.NET (C# and VB.NET) Control library.




facebook login button asp.net


Usage Examples:

Following examples show usage of Facebook Login Button ASP.NET control in two ways: default one and the one with all optional properties, including email permission and event handler, being set. For second example, C# and VB.NET code is displayed for handling event handler when user is logged in Facebook. The code from event handler executes and creates Facebook object and gets user data with Graph API, using Facebook C# SDK.

Examples of ASPX files show only the code required for registration and integration of ASP.NET control in a web page. To see all requirements for registered components, including JavaScript, CSS style and header setting, please look at the manual for page requirements. For following examples, all page requirements are placed in ASP.NET master page. Important parts for registration and integration of Facebook Invite Friends control inside ASP.NET page are highlighted.


Default Login Button:
 <%@ Page Title="" MasterPageFile="~/Master.Master" AutoEventWireup="true" Inherits="FVK_Demo.LoginButton" %>
 <%@ Register TagPrefix="fvk" TagName="loginbutton" Src="~/FVK/LoginButton.ascx" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <title>Facebook Login Button for ASP.NET</title>
     <meta name="description" content="ASP.NET login button control for Facebook Connect web sites." />
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <fvk:loginbutton ID="logginButton1" runat="server" />
 </asp:Content>


Login Button with optional properties and event handler:
 <%@ Page Title="" MasterPageFile="~/Master.Master" AutoEventWireup="true" Inherits="FVK_Demo.LoginButton" %>
 <%@ Register TagPrefix="fvk" TagName="loginbutton" Src="~/FVK/LoginButton.ascx" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <title>Facebook Login Button for ASP.NET</title>
     <meta name="description" content="ASP.NET login button control for Facebook Connect web sites." />
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <fvk:loginbutton ID="logginButton1" runat="server"
         Size="small"
         Title = "Connect with Facebook"
         Permissions="email"
         OnConnectCalled="OnConnected" 
     />
 </asp:Content>


Code behind in C#:

Following code shows the code behind for second example above, in C# programming language. When user is logged in, OnConnected method is executed. The code inside the method is using Facebook C# SDK to get user id, name, and email with Graph API.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using Facebook;
 using FVK;

 namespace FVK_Demo
 {
    public partial class LoginButton : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
             
       }

       protected void OnConnected(object sender, EventArgs e)
       {
          try
          {
             // get user data
             string accessToken = FvkAuth.AccessToken;
             var facebook = new FacebookClient(accessToken);
             JsonObject user = (JsonObject)facebook.Get("me");
             String id = (string)user["id"];
             String name = (string)user["name"];
             String email = (string)user["email"];
          }
          catch (Exception)
          {
             // Session is invalid, handle error
          }
       }
    }
 }


Code behind in VB.NET:

Following code shows the code behind, which is the same as the code above, but in VB.NET programming language. When user is logged in, OnConnected method is executed. The code inside the method is using Facebook C# SDK to get user id, name, and email with Graph API.

 Imports System.Collections.Generic
 Imports System.Linq
 Imports System.Web
 Imports System.Web.UI
 Imports System.Web.UI.WebControls
 Imports Facebook
 Imports FVK

 Namespace FVK_Demo
    Public Partial Class LoginButton
       Inherits System.Web.UI.Page
       Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
 
       End Sub

       Protected Sub OnConnected(sender As Object, e As EventArgs)
          Try
             ' get user data
             Dim accessToken As String = FvkAuth.AccessToken
             Dim facebook = New FacebookClient(accessToken)
	     Dim user As JsonObject = DirectCast(facebook.Get("me"), JsonObject)
	     Dim id As [String] = DirectCast(user("id"), String)
	     Dim name As [String] = DirectCast(user("name"), String)
	     Dim email As [String] = DirectCast(user("email"), String)
          Catch generatedExceptionName As Exception
             ' Session is invalid, handle error
          End Try
       End Sub
    End Class
 End Namespace




Configuration

Configuration of the Facebook Login Button ASP.NET control is done in ASPX page by setting optional properties and event handler. If dynamic setting of properties is required, they can be also set in the code behind, for example on Page_Load method. If event handler is used, there should be a method in the code behind to execute additional code after a user is logged in (connected with Facebook).

There is only one optional event handler which is executed when user is logged in. If user is previously logged in Facebook and Login Button is pressed, there will be no login dialog, i.e. login dialog is opened and closed immediately. In both cases, event handler is executed to execute additional code when a connection with the website and the Facebook is established.


Optional Properties:
facebook login button asp.net
Property Name
Type
Description
facebook login button asp.net
Title
String
Text inside the login button. Default value is 'Login'.
facebook login button asp.net
Size
String
Set the size of the control. Possible values: icon, small, medium, large, xlarge. Default value is 'medium'.
facebook login button asp.net
Permissions
String
Comma separated list of Facebook extended permissions. Default value is empty string.


Event Handlers:
facebook login button asp.net
Event Name
Description
facebook login button asp.net
OnConnectCalled
Name of protected method that is implemented inside the code behind and which will be called after connection with the Facebook is established.




 

Customer Testimonials

"Save time and get your product done faster with this SDK. If you are looking for a solid foundation to build a Facebook Server application, I Highly recommend this SDK and the GiftApp. With extensive code examples and broad Coverage of all things that a typical Facebook app developer needs, you’ll hit the ground running."

Nader Rahimizad, CTO GamesBox Co., Ltd.

Customer Spotlight



Vote for your favorites

Facebook development platform
Contact   |  Refund Policy   |  Privacy Policy   |  EULA