Facebook Membership Provider

Introduction

One of the great new features introduced in ASP.NET 2.0 was the use of the "Provider Model" which provides flexibility and extensibility to your Web applications. Using the provider model, we can easily extend the capabilities provided by the ASP.NET. We can extend the provider model to work with the membership & role information for the Web site users in a custom way, rather than the default SQL Server Express 2005 database. In this project we are trying to demonstrate how to use Facebook Connect featues as Membership Provider for an ASP.Net web application.

Facebook is no doubt, #1 social networking site. Facebook Connect provides Authentication service and also provides powerful set of APIs by which we can bring user's identity and connections to our web applications. Using Facebook connect can access a user's Idnetity(name, photos etc), Social Graph(friends and connections) and Stream(ctivity, distribution, and integration points within Facebook, like stream stories and Publishers)

Important Links

Live Demo : http://pinalbhatt.cloudapp.net:8081/

Source Code :  You can download the source code from Google Code or CodePlex.

To run the downloaded project you need to create facebook developer application and set its appropriate connect and callback urls. And copy the API key and the secret key to web.config files.

In this project we are using Facebook Connect as a Membership Provider for ASP.Net Web Application. This project is based on Microsoft's SDK for Facebook, available at Facebook Developer Toolkit Version 3.0

Facebook Membership Provider

As usual to create a custom membership provider, we need to inherit from MembershipProvider class and override methods from there.

FBMembership01

I have created class “FBMembershipProvider”.

We don’t need to implement following methods and properties of MembershipProvider   in our facebook membership provider as these functionalities are taken care by Facebook itself and we dont have direct access over there, hence we in our code we are just throwing NotSupportedException or returning appropriate values.

  • ChangePassword()
    ChangePasswordQuestionAndAnswer()
  • CreateUser()
  • DeleteUser()
  • FindUsersByEmail()
  • FindUsersByName()
  • GetAllUsers()
  • GetNumberOfUsersOnline()
  • GetPassword()
  • GetUserNameByEmail()
  • ResetPassword()
  • UnlockUser()
  • UpdateUser()
  • EnablePasswordReset : set to false
  • EnablePasswordRetrieval : set to false
  • MaxInvalidPasswordAttempts : set to  -1
  • MinRequiredNonAlphanumericCharacters : set to  -1
  • MinRequiredPasswordLength : set to  -1
  • PasswordAttemptWindow : set to  -1
  • PasswordFormat
  • PasswordStrengthRegularExpression
  • RequiresQuestionAndAnswer : set to false
  • RequiresUniqueEmail : set to false

I implemented only following methods:

  • GetUser(string username, bool userIsOnline)
  • GetUser(object providerUserKey, bool userIsOnline)
  • ValidateUser(string username, string password)
  • Initialize(string name, NameValueCollection config)

Another important class i created is “FBUser”, which inherits “MembershipUser” class imageand has

  • one property FacebookUser of Facebbook.Schema.user type
  • a constructor to create FBUser from Facebbook.Schema.user.

 

   1: public FBUser(Facebook.Schema.user fbsUser)
   2:     : base(Membership.Provider.Name, fbsUser.uid.ToString(), fbsUser.uid.Value,
   3:                             string.Empty, string.Empty, string.Empty, true, false,
   4:                             new DateTime(), new DateTime(), new DateTime(), new DateTime(), new DateTime())
   5: {
   6:     this.FacebookUser = fbsUser;
   7: }

2 comments

Posts a comment

Popular Posts

 
© Old - Pinal Bhatt's Blog
From the desk of Pinal Bhatt | www.PBDesk.com
Back to top