OpenID Summary1 (Facebook)

1,register the facebook app

https://developers.facebook.com/apps/188965314518142


2, Use the Facebook C# SDK (js)

http://facebooksdk.codeplex.com/wikipage?title=Getting%20Started%20with%20an%20ASP.NET%20MVC%203%20Website%20%28With%20Javascript%20SDK%29&referringTitle=Documentation

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值