Beispiel #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // since this is not a desktop app we need to send the facebook settings,
            // so that it can get apikey, api secret and post authorize url.
            FacebookAuthenticationResult = FacebookAuthenticationResult.Parse(
                HttpContext.Current.Request.Url.ToString(), FacebookContext.FacebookContext.Settings);

            DisplayAppropriateMesage();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // since this is not a desktop app we need to send the facebook settings,
            // so that it can get apikey, api secret and post authorize url.
            FacebookAuthenticationResult = FacebookAuthenticationResult.Parse(
                HttpContext.Current.Request.Url.ToString(), FacebookContext.FacebookContext.Settings);

            DisplayAppropriateMesage();
        }
 private void wbFacebookLogin_Navigated(object sender, WebBrowserNavigatedEventArgs e)
 {
     string fullPath = e.Url.ToString();
     if (fullPath.StartsWith("http://www.facebook.com/connect/login_success.html"))
     {
         FacebookAuthenticationResult = FacebookAuthenticationResult.Parse(fullPath);
         if (FacebookAuthenticationResult.IsSuccess)
             DialogResult = DialogResult.OK;
         else
             DialogResult = DialogResult.Cancel;
     }
 }
        private void wbFacebookLogin_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            string fullPath = e.Url.ToString();

            if (fullPath.StartsWith("http://www.facebook.com/connect/login_success.html"))
            {
                FacebookAuthenticationResult = FacebookAuthenticationResult.Parse(fullPath);
                if (FacebookAuthenticationResult.IsSuccess)
                {
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
        }
        public ActionResult PostAuthorize(FacebookAuthenticationResult far)
        {
            if (far.IsSuccess)
            {
                var fb   = new Facebook(far.AccessToken); // Use that access token to get the facebook user id
                var user = fb.Get <User>("/me", new Dictionary <string, string> {
                    { "fields", "id" }
                });

                // then save the access token
                FacebookMembershipProvider.LinkFacebook(User.Identity.Name, user.ID, far.AccessToken, far.ExpiresIn);

                // You may want to actually redirect instead for just returning view.
                // so that the users dont's see the ugly and confusing url like this one
                // http://localhost:30326/Facebook/PostAuthorize?code=8b2055447e53702722917_doMoY.
                // for simplicity i will just return a view.
                return(View("LinkSuccess"));
            }

            return(View("LinkError", far));
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var fbContext = filterContext.Controller as IFacebookContext;

            if (fbContext == null)
            {
                throw new FacebookSharpException(
                          "Controller must implement IFacebookContext inorder to use FacebookAuthorizeAttribute.");
            }

            string errorReason = filterContext.HttpContext.Request["error_reason"];
            string code        = filterContext.HttpContext.Request["code"];

            FacebookAuthenticationResult far;

            if (string.IsNullOrEmpty(errorReason))
            {
                if (RetriveAccessTokenManually)
                {
                    far = new FacebookAuthenticationResult(string.Empty, 0, errorReason);
                }
                else
                {
                    var    settings = fbContext.FacebookContext.Settings;
                    long   expiresIn;
                    string accessToken = Facebook.ExchangeAccessTokenForCode(code, settings.ApplicationKey, settings.ApplicationSecret,
                                                                             settings.PostAuthorizeUrl, settings.UserAgent, out expiresIn);
                    far = new FacebookAuthenticationResult(accessToken, expiresIn, errorReason);
                }
            }
            else
            {
                far = new FacebookAuthenticationResult(string.Empty, 0, errorReason);
            }

            filterContext.ActionParameters[FacebookAuthenticationResultParameterName] = far;
            filterContext.ActionParameters["code"] = code;

            base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var fbContext = filterContext.Controller as IFacebookContext;
            if (fbContext == null)
                throw new FacebookSharpException(
                    "Controller must implement IFacebookContext inorder to use FacebookAuthorizeAttribute.");

            string errorReason = filterContext.HttpContext.Request["error_reason"];
            string code = filterContext.HttpContext.Request["code"];

            FacebookAuthenticationResult far;
            if (string.IsNullOrEmpty(errorReason))
            {
                if (RetriveAccessTokenManually)
                {
                    far = new FacebookAuthenticationResult(string.Empty, 0, errorReason);
                }
                else
                {
                    var settings = fbContext.FacebookContext.Settings;
                    long expiresIn;
                    string accessToken = Facebook.ExchangeAccessTokenForCode(code, settings.ApplicationKey, settings.ApplicationSecret,
                                                                             settings.PostAuthorizeUrl, settings.UserAgent, out expiresIn);
                    far = new FacebookAuthenticationResult(accessToken, expiresIn, errorReason);
                }
            }
            else
            {
                far = new FacebookAuthenticationResult(string.Empty, 0, errorReason);
            }

            filterContext.ActionParameters[FacebookAuthenticationResultParameterName] = far;
            filterContext.ActionParameters["code"] = code;

            base.OnActionExecuting(filterContext);
        }
        public ActionResult PostAuthorize(FacebookAuthenticationResult far)
        {
            if (far.IsSuccess)
            {
                var fb = new Facebook(far.AccessToken); // Use that access token to get the facebook user id
                var user = fb.Get<User>("/me", new Dictionary<string, string> { { "fields", "id" } });

                // then save the access token
                FacebookMembershipProvider.LinkFacebook(User.Identity.Name, user.ID, far.AccessToken, far.ExpiresIn);

                // You may want to actually redirect instead for just returning view.
                // so that the users dont's see the ugly and confusing url like this one
                // http://localhost:30326/Facebook/PostAuthorize?code=8b2055447e53702722917_doMoY.
                // for simplicity i will just return a view.
                return View("LinkSuccess");
            }

            return View("LinkError", far);
        }