1,register the facebook app
https://developers.facebook.com/apps/188965314518142
2, Use the Facebook C# SDK (js)
remember just follow each step, don't use the sample(old version).
3, core code
Action LogOn
public ActionResult LogOn(string note, string email)
{
if (FacebookWebContext.Current.IsAuthenticated()) //when this client is register user & have the cookie, redirect to OpenId action
{
return RedirectToAction("OpenId", "Account");
}
................................................
Action OpenId (handle facebook login & register)
//Get: /Account/OpenId
public ActionResult OpenId() //this action is for login with facebook openid
{
var client = new FacebookWebClient();
dynamic me = client.Get("me"); //get the current client info
if (FacebookWebContext.Current.IsAuthenticated())
{
if (Membership.ValidateUser(me.email, me.id)) //if user already register as facebook
{
FormsAuthentication.SetAuthCookie(me.email, true); // set cookie
return RedirectToAction("Index", "Home"); // return to home page
}
else // if not exister, register it
{
MembershipCreateStatus createStatus;
Membership.CreateUser(me.name, me.id, me.email, null, null, true, null, out createStatus); // register
if (createStatus == MembershipCreateStatus.Success) // if register successful
{
FormsAuthentication.SetAuthCookie(me.email, true); // set cookie
return RedirectToAction("Index", "Home"); // return to home
}
}
return RedirectToAction("Index", "Home");
}
return RedirectToAction("Index", "Home");
}
Login button at view page (scope for control access scope)
<fb:login-button autologoutlink='true' scope='email,read_stream,publish_stream,read_friendlists,user_activities'>Login With Facebook</fb:login-button>
FAQ:
1, don't link the facebook js sdk more than once, if that would be crash the login button