// // 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 void BeginAuthorization_Sets_Callback() { var auth = new MvcAuthorizer(); Uri linqToTwitterUri = new Uri("http://linqtotwitter.codeplex.com"); auth.BeginAuthorization(linqToTwitterUri); Assert.Equal(linqToTwitterUri, auth.Callback); }
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); }
public ActionResult Index() { IOAuthCredentials credentials = new SessionStateCredentials(); MvcAuthorizer auth; TwitterContext twitterCtx; if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null) { credentials.ConsumerKey = ConfigurationManager.AppSettings[CommonConstants.ConsumerKey]; credentials.ConsumerSecret = ConfigurationManager.AppSettings[CommonConstants.ConsumerSecret]; credentials.AccessToken = ConfigurationManager.AppSettings[CommonConstants.AccessToken]; credentials.OAuthToken = ConfigurationManager.AppSettings[CommonConstants.OAuthToken]; credentials.ScreenName = ConfigurationManager.AppSettings[CommonConstants.ScreenName]; } 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); var friendTweets = (from tweet in twitterCtx.Status where tweet.Type == StatusType.Home select new TwitterEntity { ScreenName = tweet.User.Name, 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; }
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 }; auth.CompleteAuthorization(Request.Url); if (!auth.IsAuthorized) { Uri specialUri = new Uri(Request.Url.ToString()); return auth.BeginAuthorization(specialUri); } twitterCtx = new TwitterContext(auth); var friendTweets = (from tweet in twitterCtx.Status where tweet.Type == StatusType.User && tweet.ScreenName == "AndreMayo1" select new TweetViewModel { ImageUrl = tweet.User.ProfileImageUrl, ScreenName = tweet.User.Identifier.ScreenName, Tweet = tweet.Text }) .ToList(); return View(friendTweets); }