diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/HomeController.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/HomeController.cs index 4a791db6..d8741e38 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/HomeController.cs +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/HomeController.cs @@ -16,8 +16,8 @@ public HomeController(ILogger logger) public IActionResult Index() { - if (!new SessionStateCredentialStore(HttpContext.Session).HasAllCredentials()) - return RedirectToAction("Index", "OAuth"); + if (!new OAuth2SessionCredentialStore(HttpContext.Session).HasAllCredentials()) + return RedirectToAction("Index", "OAuth2"); return View(); } diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuth2Controller.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuth2Controller.cs new file mode 100644 index 00000000..8817b6c9 --- /dev/null +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuth2Controller.cs @@ -0,0 +1,75 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http.Extensions; +using LinqToTwitter.OAuth; +using Microsoft.Extensions.Primitives; +using System.Collections.Generic; + +namespace LinqToTwitter.MVC.CSharp.Controllers +{ + public class OAuth2Controller : Controller + { + private readonly ILogger logger; + + public OAuth2Controller(ILogger logger) + { + this.logger = logger; + } + + public ActionResult Index() + { + return View(); + } + + public async Task BeginAsync() + { + string twitterCallbackUrl = Request.GetDisplayUrl().Replace("Begin", "Complete"); + + var auth = new MvcOAuth2Authorizer + { + CredentialStore = new OAuth2SessionCredentialStore(HttpContext.Session) + { + ClientID = Environment.GetEnvironmentVariable(OAuthKeys.TwitterClientID), + ClientSecret = Environment.GetEnvironmentVariable(OAuthKeys.TwitterClientSecret), + Scopes = new List + { + "tweet.read", + "tweet.write", + "tweet.moderate.write", + "users.read", + "follows.read", + "follows.write", + "offline.access", + "space.read", + "mute.read", + "mute.write", + "like.read", + "like.write", + "block.read", + "block.write" + }, + RedirectUri = twitterCallbackUrl, + } + }; + + return await auth.BeginAuthorizeAsync("MyState"); + } + + public async Task CompleteAsync() + { + var auth = new MvcOAuth2Authorizer + { + CredentialStore = new OAuth2SessionCredentialStore(HttpContext.Session) + }; + + Request.Query.TryGetValue("code", out StringValues code); + Request.Query.TryGetValue("state", out StringValues state); + + await auth.CompleteAuthorizeAsync(code, state); + + return RedirectToAction("Index", "Home"); + } + } +} diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuthController.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuthController.cs index a1652de5..9cce29c0 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuthController.cs +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuthController.cs @@ -8,11 +8,11 @@ namespace LinqToTwitter.MVC.CSharp.Controllers { public class OAuthController : Controller { - private readonly ILogger _logger; + private readonly ILogger logger; public OAuthController(ILogger logger) { - _logger = logger; + this.logger = logger; } public ActionResult Index() @@ -36,7 +36,7 @@ public async Task BeginAsync() //var parameters = new Dictionary { { "my_custom_param", "val" } }; //string twitterCallbackUrl = Request.GetDisplayUrl().Replace("Begin", "Complete"); //return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl), parameters); - + await auth.CredentialStore.ClearAsync(); string twitterCallbackUrl = Request.GetDisplayUrl().Replace("Begin", "Complete"); return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl)); } diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/StatusDemosController.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/TweetDemosController.cs similarity index 52% rename from Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/StatusDemosController.cs rename to Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/TweetDemosController.cs index 4ebb565c..43505bfd 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/StatusDemosController.cs +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/TweetDemosController.cs @@ -1,4 +1,5 @@ using LinqToTwitter; +using LinqToTwitter.Common; using LinqToTwitter.MVC.CSharp.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -10,11 +11,11 @@ namespace MvcDemo.Controllers { - public class StatusDemosController : Controller + public class TweetDemosController : Controller { readonly IWebHostEnvironment webHostEnvironment; - public StatusDemosController(IWebHostEnvironment webHostEnvironment) + public TweetDemosController(IWebHostEnvironment webHostEnvironment) { this.webHostEnvironment = webHostEnvironment; } @@ -38,14 +39,14 @@ public ActionResult Tweet() [ActionName("Tweet")] public async Task TweetAsync(SendTweetViewModel tweet) { - var auth = new MvcAuthorizer + var auth = new MvcOAuth2Authorizer { - CredentialStore = new SessionStateCredentialStore(HttpContext.Session) + CredentialStore = new OAuth2SessionCredentialStore(HttpContext.Session) }; var ctx = new TwitterContext(auth); - Status responseTweet = await ctx.TweetAsync(tweet.Text); + Tweet responseTweet = await ctx.TweetAsync(tweet.Text); var responseTweetVM = new SendTweetViewModel { @@ -56,27 +57,44 @@ public async Task TweetAsync(SendTweetViewModel tweet) return View(responseTweetVM); } - [ActionName("HomeTimeline")] - public async Task HomeTimelineAsync() + [ActionName("TweetTimeline")] + public async Task TweetTimelineAsync() { - var auth = new MvcAuthorizer + var auth = new MvcOAuth2Authorizer { - CredentialStore = new SessionStateCredentialStore(HttpContext.Session) + CredentialStore = new OAuth2SessionCredentialStore(HttpContext.Session) }; var ctx = new TwitterContext(auth); - var tweets = + var userQuery = + await + (from usr in ctx.TwitterUser + where usr.Type == UserType.UsernameLookup && + usr.Usernames == "Linq2Twitr" && + usr.UserFields == UserField.ProfileImageUrl + select usr) + .SingleOrDefaultAsync(); + + TwitterUser user = userQuery.Users.FirstOrDefault(); + + var tweetQuery = await - (from tweet in ctx.Status - where tweet.Type == StatusType.Home + (from tweet in ctx.Tweets + where tweet.Type == TweetType.TweetsTimeline && + tweet.ID == user.ID.ToString() + select tweet) + .SingleOrDefaultAsync(); + + var tweets = + (from tweet in tweetQuery.Tweets select new TweetViewModel { - ImageUrl = tweet.User.ProfileImageUrl, - ScreenName = tweet.User.ScreenNameResponse, - Text = tweet.Text + ImageUrl = user.ProfileImageUrl, + ScreenName = user.Name, + Text = tweet.Text }) - .ToListAsync(); + .ToList(); return View(tweets); } @@ -84,12 +102,12 @@ public async Task HomeTimelineAsync() [ActionName("UploadImage")] public async Task UploadImageAsync() { - var auth = new MvcAuthorizer + var auth = new MvcOAuth2Authorizer { - CredentialStore = new SessionStateCredentialStore(HttpContext.Session) + CredentialStore = new OAuth2SessionCredentialStore(HttpContext.Session) }; - var twitterCtx = new TwitterContext(auth); + var ctx = new TwitterContext(auth); string status = $"Testing multi-image tweet #Linq2Twitter £ {DateTime.Now}"; string mediaCategory = "tweet_image"; @@ -102,23 +120,23 @@ public async Task UploadImageAsync() var imageUploadTasks = new List> { - twitterCtx.UploadMediaAsync(System.IO.File.ReadAllBytes(path), "image/jpg", mediaCategory), + ctx.UploadMediaAsync(System.IO.File.ReadAllBytes(path), "image/jpg", mediaCategory), }; await Task.WhenAll(imageUploadTasks); - List mediaIds = + List mediaIds = (from tsk in imageUploadTasks - select tsk.Result.MediaID) + select tsk.Result.MediaID.ToString()) .ToList(); - Status tweet = await twitterCtx.TweetAsync(status, mediaIds); + Tweet tweet = await ctx.TweetMediaAsync(status, mediaIds); return View( - new TweetViewModel + new MediaViewModel { - ImageUrl = tweet.User.ProfileImageUrl, - ScreenName = tweet.User.ScreenNameResponse, + MediaUrl = tweet.Entities.Urls.FirstOrDefault()?.Url, + Description = tweet.Entities.Urls.FirstOrDefault()?.Description, Text = tweet.Text }); } diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp.csproj b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp.csproj index 3335898f..d5a1529d 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp.csproj +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp.csproj @@ -5,7 +5,16 @@ - + + + + + + ..\..\..\..\..\src\LinqToTwitter6\LinqToTwitter.AspNet\bin\Debug\net5.0\LinqToTwitter.dll + + + ..\..\..\..\..\src\LinqToTwitter6\LinqToTwitter.AspNet\bin\Debug\net5.0\LinqToTwitter.AspNet.dll + diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Models/MediaViewModel.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Models/MediaViewModel.cs new file mode 100644 index 00000000..347fb7e2 --- /dev/null +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Models/MediaViewModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace LinqToTwitter.MVC.CSharp.Models +{ + public class MediaViewModel + { + [DisplayName("Image")] + [DataType(DataType.ImageUrl)] + public string MediaUrl { get; set; } + + [DisplayName("Screen Name")] + public string Description { get; set; } + + [DisplayName("Tweet")] + public string Text { get; set; } + } +} diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Properties/launchSettings.json b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Properties/launchSettings.json index 6b676099..a2b1728b 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Properties/launchSettings.json +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Properties/launchSettings.json @@ -3,7 +3,7 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:62333", + "applicationUrl": "http://127.0.0.1:62333", "sslPort": 44395 } }, @@ -19,7 +19,7 @@ "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://127.0.0.1:5001;http://127.0.0.1:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Startup.cs b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Startup.cs index 4b992f02..a0b42e0a 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Startup.cs +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Startup.cs @@ -25,7 +25,6 @@ public void ConfigureServices(IServiceCollection services) services.AddSession(options => { - options.IdleTimeout = TimeSpan.FromSeconds(10); options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth/index.cshtml b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth/index.cshtml index 14ef4f8a..8be7b2b2 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth/index.cshtml +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth/index.cshtml @@ -1,7 +1,7 @@ @{ - ViewBag.Title = "Authorizing Application"; + ViewBag.Title = "Authorizing Application with OAuth 1.0A"; } -

Authorize with OAuth

+

Authorize with OAuth 1.0A

@Html.ActionLink("Begin the Authorization Process", "Begin") diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth2/Index.cshtml b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth2/Index.cshtml new file mode 100644 index 00000000..aca3a761 --- /dev/null +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth2/Index.cshtml @@ -0,0 +1,7 @@ +@{ + ViewBag.Title = "Authorizing Application with OAuth 2.0"; +} + +

Authorize with OAuth 2.0

+@Html.ActionLink("Begin the Authorization Process", "Begin") + diff --git a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/Shared/_Layout.cshtml b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/Shared/_Layout.cshtml index 2b4dd454..fd109c30 100644 --- a/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/Shared/_Layout.cshtml +++ b/Samples/LinqToTwitter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/Shared/_Layout.cshtml @@ -22,7 +22,7 @@ Home