Beispiel #1
0
        public LogOnModel facebookLogin(string returnUrl, string code, string referral,string userType)
        {
            var model = new LogOnModel();

            FacebookService FacebookService = new FacebookService();
            model = FacebookService.Login(returnUrl, code, referral, userType);

            return model;
        }
        public ActionResult FBLogin(string type)
        {
            var response = new ResponseModel<string>();

            String code = Request.QueryString["code"];
            string app_id = string.Empty;
            string app_secret = string.Empty;
            string returnUrl = "http://"+Request.Url.Authority+"/SocialAuth/FBLogin/facebook/";
            if(Request.Url.Authority.Contains("localhost"))
            {
                app_id = ConfigurationManager.AppSettings["FacebookAppID"].ToString();
                app_secret = ConfigurationManager.AppSettings["FacebookAppSecret"].ToString();
            }
            else
            {
                app_id = ConfigurationManager.AppSettings["FacebookAppIDCautom"].ToString();
                app_secret = ConfigurationManager.AppSettings["FacebookAppSecretCautom"].ToString();
            }
            

            string scope = "";
            if (code == null)
            {
                response.Status = 199;
                response.Message = "reload page with given url";
                response.Payload = (string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, returnUrl, scope));
                
                //return Json(response,JsonRequestBehavior.AllowGet);
                Response.Redirect(response.Payload);
            }
            else
            {
                try
                {
                    string access_token = new FacebookService().getFacebookAuthToken(returnUrl, scope, code, app_id, app_secret);
                    var fb = new FacebookClient(access_token);
                    //dynamic result = fb.Get("fql",
                    //            new { q = "SELECT uid, name, first_name, middle_name, last_name, sex, locale, pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo, username FROM user WHERE uid=me()" });

                    dynamic fqlResponse = fb.Get("fql",
                                new { q = "SELECT uid, username FROM user WHERE uid=me()" });
                    var FacebookAuthData = new FacebookAuth();
                    string fid = Convert.ToString(fqlResponse.data[0].uid);
                    FacebookAuthData.username = Constants.NA;
                    FacebookAuthData.AuthToken = access_token;
                    FacebookAuthData.datetime = DateTime.Now.ToString();
                    FacebookAuthData.facebookId = Convert.ToString(fqlResponse.data[0].uid);
                    FacebookAuthData.facebookUsername = fqlResponse.data[0].username;

                    var ifAlreadyExists = _db.FacebookAuths.SingleOrDefault(x => x.facebookId == fid);
                    if (ifAlreadyExists == null)
                    {                        
                        _db.FacebookAuths.Add(FacebookAuthData);
                    }
                    else
                    {
                        // refresh the token
                        ifAlreadyExists.AuthToken = access_token;
                        ifAlreadyExists.datetime = DateTime.Now.ToString();
                    }
                    try
                    {
                        _db.SaveChanges();                        
                        response.Status = 200;
                        response.Message = "success-";                        
                    }
                    catch (DbEntityValidationException e)
                    {
                        DbContextException.LogDbContextException(e);
                        response.Status = 500;
                        response.Message = "Failed";                        
                    }

                    ViewBag.facebookId = fqlResponse.data[0].uid;
                    return View(FacebookAuthData);
                }
                catch (Exception ex)
                {

                    logger.Error("Error Occured while getting Facebook Auth Token",ex);
                }
                
                //var fb = new FacebookClient();
                //dynamic result = fb.Get("fql",
                    //new { q = "SELECT page_id FROM page_fan WHERE uid=100001648098091 AND page_id=223215721036909" });  
                
 
                //To obtain an App Access Token, invoke the following HTTP GET request

                //GET https://graph.facebook.com/oauth/access_token?
                //            client_id=YOUR_APP_ID
                //           &client_secret=YOUR_APP_SECRET
                //           &grant_type=client_credentials

                //The API will respond with a query-string formatted string of the form:

                //    access_token=YOUR_APP_ACCESS_TOKEN
            }
            return View();
        }