Beispiel #1
0
        public ActionResult MyProfile(string twitterUserId)
        {
            UserModel     mUser = null;
            MvcAuthorizer auth  = null;

            try
            {
                auth = GetAuthorizer(twitterUserId);

                auth.CompleteAuthorization(Request.Url);

                if (!auth.IsAuthorized)
                {
                    var specialUri = new Uri("/Home/MyProfile");
                    return(auth.BeginAuthorization(specialUri));
                }

                var twitterCtx = new TwitterContext(auth);

                mUser = GetCurrentUser(twitterCtx, auth, twitterUserId);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMsg = ex.ToString() + " twitterUserId: " + twitterUserId;
            }


            return(View(mUser));
        }
Beispiel #2
0
        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            //return View();
            if (!Request.IsAuthenticated)
            {
                var credentials = new InMemoryCredentials();
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["TwitterCustomerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["TwitterCustomerSecret"];
                var auth = new MvcAuthorizer {
                    Credentials = credentials
                };
                auth.CompleteAuthorization(Request.Url);
                if (!auth.IsAuthorized)
                {
                    return(auth.BeginAuthorization(Request.Url));
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(auth.ScreenName, true);
                    PostworthyUser pm = UsersCollection.Single(auth.ScreenName, addIfNotFound: true);
                    if (string.IsNullOrEmpty(pm.AccessToken) && string.IsNullOrEmpty(pm.OAuthToken))
                    {
                        pm.AccessToken = auth.Credentials.AccessToken;
                        pm.OAuthToken  = auth.Credentials.OAuthToken;
                        UsersCollection.Save();
                    }
                    return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
                }
            }
            else
            {
                return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
            }
        }
        public ActionResult Index()
        {
            /************** Twitter authorize procedures *********************************/

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer {
                Credentials = credentials
            };
            auth.CompleteAuthorization(Request.Url);

            if (!auth.IsAuthorized)
            {
                Uri specialUri = new Uri(Request.Url.ToString());
                return(auth.BeginAuthorization(specialUri));
            }
            twitterCtx = new TwitterContext(auth);

            /***************************************************************************/

            indexModel = new IndexModels();

            return(View());
        }
        public void BeginAuthorization_Sets_Callback()
        {
            var auth             = new MvcAuthorizer();
            Uri linqToTwitterUri = new Uri("http://linqtotwitter.codeplex.com");

            auth.BeginAuthorization(linqToTwitterUri);

            Assert.Equal(linqToTwitterUri, auth.Callback);
        }
Beispiel #5
0
        public ActionResult ExternalLogin(string returnUrl)
        {
            MvcAuthorizer auth             = GetAuthorizer(null);
            var           twitterReturnUrl = ConfigurationManager.AppSettings["TwitterAutorizationReturnUrl"];


            if (!auth.CompleteAuthorization(Request.Url))
            {
                var          specialUri = new Uri(twitterReturnUrl);
                ActionResult res        = auth.BeginAuthorization(specialUri);
                return(res);
            }

            return(AppAuthorizationConfirmation(null));
        }
Beispiel #6
0
        public ActionResult BeginAuthorization(string query)
        {
            var authorizer = new MvcAuthorizer
            {
                Credentials = new SessionStateCredentials
                {
                    ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]
                }
            };

            var url = Url.Action("CompleteAuthorization", "OAuth", new {query}, Uri.UriSchemeHttps);
            var callback = new Uri(url);
            return authorizer.IsAuthorized
                       ? RedirectToAction("CompleteAuthorization")
                       : authorizer.BeginAuthorization(callback);
        }
Beispiel #7
0
        public ActionResult GetNewCompetitor(string twitterUserId)
        {
            MvcAuthorizer auth = GetAuthorizer(twitterUserId);

            auth.CompleteAuthorization(Request.Url);

            if (!auth.IsAuthorized)
            {
                var specialUri = new Uri("/Home/AppAuthorizationConfirmation");
                return(auth.BeginAuthorization(specialUri));
            }

            var twitterCtx = new TwitterContext(auth);
            var competitor = FriendSelector(twitterCtx, twitterUserId);

            return(Json(competitor));
        }
        public ActionResult Index()
        {
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            // internally, this doesn't execute if BeginAuthorization hasn't been called yet
            //  but it will execute after the user authorizes your application
            auth.CompleteAuthorization(Request.Url);

            // this will only execute if we don't have all 4 keys, which is what IsAuthorized checks
            if (!auth.IsAuthorized)
            {
                Uri specialUri = new Uri(Request.Url.ToString());

                // url param is optional, it lets you specify the page Twitter redirects to.
                // You can use it to complete the OAuth process on another action/controller - in which
                // case you would move auth.CompleteAuthorization to that action/controller.
                return(auth.BeginAuthorization(specialUri));
            }

            twitterCtx = new TwitterContext(auth);

            List <TweetViewModel> friendTweets =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.User &&
                 tweet.ScreenName == "JoeMayo"
                 select new TweetViewModel
            {
                ImageUrl = tweet.User.ProfileImageUrl,
                ScreenName = tweet.User.Identifier.ScreenName,
                Tweet = tweet.Text
            })
                .ToList();

            return(View(friendTweets));
        }
Beispiel #9
0
        private ActionResult GetTweets(string screenName)
        {
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            auth.CompleteAuthorization(Request.Url);

            if (!auth.IsAuthorized)
            {
                Uri specialUri = new Uri(Request.Url.ToString());
                return(auth.BeginAuthorization(specialUri));
            }
            IEnumerable <TweetViewModel> friendTweets = new List <TweetViewModel>();

            if (string.IsNullOrEmpty(screenName))
            {
                return(View(friendTweets));
            }
            twitterCtx   = new TwitterContext(auth);
            friendTweets =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.User &&
                 tweet.ScreenName == screenName &&
                 tweet.IncludeEntities == true
                 select new TweetViewModel
            {
                ImageUrl = tweet.User.ProfileImageUrl,
                ScreenName = tweet.User.Identifier.ScreenName,
                MediaUrl = GetTweetMediaUrl(tweet),
                Tweet = tweet.Text
            })
                .ToList();
            return(View(friendTweets));
        }
        private ActionResult Authorize()
        {
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            auth.CompleteAuthorization(Request.Url);

            if (!auth.IsAuthorized)
            {
                Uri specialUri = new Uri(Request.Url.ToString());
                return(auth.BeginAuthorization(specialUri));
            }
            ViewBag.User = auth.Credentials.ScreenName;
            return(null);
        }