Skip to content

Commit

Permalink
Added ASP.NET Core demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeMayo committed Dec 16, 2017
1 parent e248b5a commit 0981dfc
Show file tree
Hide file tree
Showing 74 changed files with 23,833 additions and 41 deletions.
Expand Up @@ -146,30 +146,38 @@
<add name="http" />
</listenerAdapters>
<sites>
<site name="MvcDemo" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\MvcDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5287:localhost" />
</bindings>
</site>
<site name="WebFormsDemo" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\WebFormsDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:17008:localhost" />
</bindings>
</site>
<site name="AccountActivityDemo" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\AccountActivityDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:10291:localhost" />
</bindings>
</site>
<site name="MvcDemo" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\MvcDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5287:localhost" />
</bindings>
</site>
<site name="WebFormsDemo" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\WebFormsDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:17008:localhost" />
</bindings>
</site>
<site name="AccountActivityDemo" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\AccountActivityDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:10291:localhost" />
</bindings>
</site>
<site name="CoreDemo" id="4">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Projects\LinqToTwitter\Samples\net46\CSharp\AspNetSamples\CoreDemo" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:13897:localhost" />
</bindings>
</site>
<siteDefaults>
<logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" />
<traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" />
Expand Down Expand Up @@ -999,4 +1007,17 @@
</security>
</system.webServer>
</location>
<location path="CoreDemo">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false" />
<httpCompression>
<dynamicCompression>
<add mimeType="text/event-stream" enabled="false" />
</dynamicCompression>
</httpCompression>
</system.webServer>
</location>
</configuration>
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand All @@ -12,19 +13,13 @@ namespace AccountActivityDemo.Controllers
{
public class AccountActivityController : ApiController
{
string consumerKey = "";
string consumerSecret = "";
string accessToken = "";
string accessTokenSecret = "";
string apiKey = "";

ulong chatbotID = 15411837;

public object Get(string crc_token)
{
return new
{
response_token = new AccountActivity().BuildCrcResponse(crc_token, consumerSecret)
response_token = new AccountActivity().BuildCrcResponse(crc_token, ConfigurationManager.AppSettings["consumerSecret"])
};
}

Expand All @@ -37,7 +32,7 @@ public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
Monitor.Enter(dmReadLock);
string response = await request.Content.ReadAsStringAsync();

if (!new AccountActivity().IsValidPostSignature(request, response, consumerSecret))
if (!new AccountActivity().IsValidPostSignature(request, response, ConfigurationManager.AppSettings["consumerSecret"]))
return request.CreateResponse(HttpStatusCode.Unauthorized);

JObject content = JObject.Parse(response);
Expand All @@ -54,10 +49,10 @@ public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
OAuthToken = accessToken,
OAuthTokenSecret = accessTokenSecret
ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
OAuthToken = ConfigurationManager.AppSettings["accessToken"],
OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
}
};
var twitterCtx = new TwitterContext(authorizer);
Expand Down
10 changes: 10 additions & 0 deletions Samples/net46/CSharp/AspNetSamples/AccountActivityDemo/Web.config
Expand Up @@ -9,6 +9,16 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

<!-- Fill in your consumer key and secret here to make the OAuth sample work. -->
<!-- Twitter sign-up: https://dev.twitter.com/ -->
<add key="consumerKey" value=""/>
<add key="consumerSecret" value=""/>
<add key="oauthToken" value=""/>
<add key="oauthTokenSecret" value=""/>
<add key="accessToken" value=""/>
<add key="accessTokenSecret" value=""/>
<add key="apiKey" value=""/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
Expand Down
16 changes: 15 additions & 1 deletion Samples/net46/CSharp/AspNetSamples/AspNetSamples.sln
@@ -1,14 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcDemo", "MvcDemo\MvcDemo.csproj", "{096E2905-36BE-4CA3-A5B9-94870522D447}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebFormsDemo", "WebFormsDemo\WebFormsDemo.csproj", "{2B50862D-1D21-4B8A-978E-DC289FC196EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountActivityDemo", "AccountActivityDemo\AccountActivityDemo.csproj", "{C2BC4014-A430-4B61-AC2E-5006BC2CE01D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreDemo", "CoreDemo\CoreDemo.csproj", "{44AA382E-9A56-4146-A3B9-1CC753D87C05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,18 @@ Global
{C2BC4014-A430-4B61-AC2E-5006BC2CE01D}.Release|x64.Build.0 = Release|Any CPU
{C2BC4014-A430-4B61-AC2E-5006BC2CE01D}.Release|x86.ActiveCfg = Release|Any CPU
{C2BC4014-A430-4B61-AC2E-5006BC2CE01D}.Release|x86.Build.0 = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|x64.ActiveCfg = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|x64.Build.0 = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|x86.ActiveCfg = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Debug|x86.Build.0 = Debug|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|Any CPU.Build.0 = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|x64.ActiveCfg = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|x64.Build.0 = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|x86.ActiveCfg = Release|Any CPU
{44AA382E-9A56-4146-A3B9-1CC753D87C05}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CoreDemo.Models;
using LinqToTwitter;

namespace CoreDemo.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
if (!new SessionStateCredentialStore(HttpContext.Session).HasAllCredentials())
return RedirectToAction("Index", "OAuth");

return View();
}

public IActionResult About()
{
ViewData["Message"] = "Your application description page.";

return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
@@ -0,0 +1,69 @@
using System;
using System.Threading.Tasks;
using LinqToTwitter;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace CoreDemo.Controllers
{
public class OAuthController : Controller
{
readonly IConfiguration configuration;

public OAuthController(IConfiguration configuration)
{
this.configuration = configuration;
}

public ActionResult Index()
{
return View();
}

public async Task<ActionResult> Begin()
{
//var auth = new MvcSignInAuthorizer
var auth = new MvcAuthorizer
{
CredentialStore = new SessionStateCredentialStore(HttpContext.Session)
{
ConsumerKey = configuration["Twitter:ConsumerKey"],
ConsumerSecret = configuration["Twitter:ConsumerSecret"]
}
};

string twitterCallbackUrl = Request.GetDisplayUrl().Replace("Begin", "Complete");
return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl));
}

public async Task<ActionResult> Complete()
{
var auth = new MvcAuthorizer
{
CredentialStore = new SessionStateCredentialStore(HttpContext.Session)
};

await auth.CompleteAuthorizeAsync(new Uri(Request.GetDisplayUrl()));

// This is how you access credentials after authorization.
// The oauthToken and oauthTokenSecret do not expire.
// You can use the userID to associate the credentials with the user.
// You can save credentials any way you want - database,
// isolated storage, etc. - it's up to you.
// You can retrieve and load all 4 credentials on subsequent
// queries to avoid the need to re-authorize.
// When you've loaded all 4 credentials, LINQ to Twitter will let
// you make queries without re-authorizing.
//
//var credentials = auth.CredentialStore;
//string oauthToken = credentials.OAuthToken;
//string oauthTokenSecret = credentials.OAuthTokenSecret;
//string screenName = credentials.ScreenName;
//ulong userID = credentials.UserID;
//

return RedirectToAction("Index", "Home");
}
}
}

0 comments on commit 0981dfc

Please sign in to comment.