Facebook Login Button for ASP.NETFacebook 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.
![]() 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.
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).
Optional Properties:
Property Name
Type
Description
Title
String
Text inside the login button. Default value is 'Login'.
Size
String
Set the size of the control. Possible values: icon, small, medium, large, xlarge. Default value is 'medium'.
Permissions
String
Comma separated list of Facebook extended permissions. Default value is empty string.
Event Handlers:
Event Name
Description
OnConnectCalled
Name of protected method that is implemented inside the code behind and which will be called after connection with the Facebook is established.
|