-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed initial support for OAuth2 (#252)
* Working on OAuth2 * Working on OAuth2. * Completed initial OAuth2 support for ASP.NET MVC.
- Loading branch information
Showing
38 changed files
with
1,207 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Controllers/OAuth2Controller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OAuth2Controller> logger; | ||
|
||
public OAuth2Controller(ILogger<OAuth2Controller> logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
public ActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
public async Task<ActionResult> 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<string> | ||
{ | ||
"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<ActionResult> 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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...itter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Models/MediaViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...itter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth/index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
@{ | ||
ViewBag.Title = "Authorizing Application"; | ||
ViewBag.Title = "Authorizing Application with OAuth 1.0A"; | ||
} | ||
|
||
<h2>Authorize with OAuth</h2> | ||
<h2>Authorize with OAuth 1.0A</h2> | ||
@Html.ActionLink("Begin the Authorization Process", "Begin") | ||
|
7 changes: 7 additions & 0 deletions
7
...tter6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/OAuth2/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@{ | ||
ViewBag.Title = "Authorizing Application with OAuth 2.0"; | ||
} | ||
|
||
<h2>Authorize with OAuth 2.0</h2> | ||
@Html.ActionLink("Begin the Authorization Process", "Begin") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
.../ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/StatusDemos/Index.cshtml
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
...6/ASP.NET/LinqToTwitter.MVC.CSharp/LinqToTwitter.MVC.CSharp/Views/TweetDemos/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@{ | ||
ViewBag.Title = "Tweet Demos"; | ||
} | ||
|
||
<h2>Tweet Demos</h2> | ||
|
||
<p>@Html.ActionLink("Tweet Demo", "Tweet")</p> | ||
|
||
<p>@Html.ActionLink("Tweet Timeline Demo", "TweetTimeline")</p> | ||
|
||
<p>@Html.ActionLink("Upload Image Demo", "UploadImage")</p> |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...arp/Views/StatusDemos/HomeTimeline.cshtml → ...arp/Views/TweetDemos/TweetTimeline.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.