From ab2a0d4be617f8014b2298832e6cd99c038d9e5f Mon Sep 17 00:00:00 2001 From: "Jean.R" Date: Sun, 13 Aug 2017 22:40:52 +0200 Subject: [PATCH 1/4] - Upgrade to .Net Core 2.0 Preview 2 - Sample client (Javascript/JQuery) added Note: Current .Net Core 2.0 RTM dropped support for the JwtToken, expected to be back soon. --- TokenAuthExample.sln | 11 +- global.json | 5 - .../Controllers/TokenController.cs | 4 - .../Controllers/ValuesController.cs | 3 - src/TokenAuthExampleWebApplication/Program.cs | 4 - .../Project_Readme.html | 203 ------------------ .../Properties/launchSettings.json | 2 +- .../RSAKeyUtils.cs | 4 - src/TokenAuthExampleWebApplication/Startup.cs | 47 ++-- .../TokenAuthExampleWebApplication.csproj | 16 ++ .../TokenAuthExampleWebApplication.xproj | 19 -- .../TokenAuthOptions.cs | 5 - .../project.json | 64 ------ test/client/javascript/index.html | 88 ++++++++ 14 files changed, 136 insertions(+), 339 deletions(-) delete mode 100644 global.json delete mode 100644 src/TokenAuthExampleWebApplication/Project_Readme.html create mode 100644 src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj delete mode 100644 src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.xproj delete mode 100644 src/TokenAuthExampleWebApplication/project.json create mode 100644 test/client/javascript/index.html diff --git a/TokenAuthExample.sln b/TokenAuthExample.sln index 2dc4dbf..c93263a 100644 --- a/TokenAuthExample.sln +++ b/TokenAuthExample.sln @@ -1,17 +1,15 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.23107.0 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4A19A4CA-58C8-4EE5-94BA-B2D2F2DD68EE}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8165327B-8AB8-4B19-8AC0-9251BFDC1BDC}" ProjectSection(SolutionItems) = preProject - global.json = global.json NuGet.Config = NuGet.Config EndProjectSection EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TokenAuthExampleWebApplication", "src\TokenAuthExampleWebApplication\TokenAuthExampleWebApplication.xproj", "{F6B6DAA2-0460-419E-88D7-CE887777F469}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokenAuthExampleWebApplication", "src\TokenAuthExampleWebApplication\TokenAuthExampleWebApplication.csproj", "{F6B6DAA2-0460-419E-88D7-CE887777F469}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -30,4 +28,7 @@ Global GlobalSection(NestedProjects) = preSolution {F6B6DAA2-0460-419E-88D7-CE887777F469} = {4A19A4CA-58C8-4EE5-94BA-B2D2F2DD68EE} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D0B6A60A-7143-4EA9-9A05-D39C63E65A6B} + EndGlobalSection EndGlobal diff --git a/global.json b/global.json deleted file mode 100644 index e40f923..0000000 --- a/global.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "projects": [ "src", "test" ], - "sdk": { - } -} diff --git a/src/TokenAuthExampleWebApplication/Controllers/TokenController.cs b/src/TokenAuthExampleWebApplication/Controllers/TokenController.cs index 1a5b944..4fe00a4 100644 --- a/src/TokenAuthExampleWebApplication/Controllers/TokenController.cs +++ b/src/TokenAuthExampleWebApplication/Controllers/TokenController.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using System.IdentityModel.Tokens; using System.Security.Claims; using System.Security.Principal; using System.IdentityModel.Tokens.Jwt; @@ -17,8 +15,6 @@ public class TokenController : Controller public TokenController(TokenAuthOptions tokenOptions) { this.tokenOptions = tokenOptions; - //this.bearerOptions = options.Value; - //this.signingCredentials = signingCredentials; } /// diff --git a/src/TokenAuthExampleWebApplication/Controllers/ValuesController.cs b/src/TokenAuthExampleWebApplication/Controllers/ValuesController.cs index 07e3c25..85c490c 100644 --- a/src/TokenAuthExampleWebApplication/Controllers/ValuesController.cs +++ b/src/TokenAuthExampleWebApplication/Controllers/ValuesController.cs @@ -1,9 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace TokenAuthExampleWebApplication.Controllers { diff --git a/src/TokenAuthExampleWebApplication/Program.cs b/src/TokenAuthExampleWebApplication/Program.cs index f1a3014..4f316d3 100644 --- a/src/TokenAuthExampleWebApplication/Program.cs +++ b/src/TokenAuthExampleWebApplication/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; diff --git a/src/TokenAuthExampleWebApplication/Project_Readme.html b/src/TokenAuthExampleWebApplication/Project_Readme.html deleted file mode 100644 index 1d5eda6..0000000 --- a/src/TokenAuthExampleWebApplication/Project_Readme.html +++ /dev/null @@ -1,203 +0,0 @@ - - - - - Welcome to ASP.NET 5 - - - - - - - - - - diff --git a/src/TokenAuthExampleWebApplication/Properties/launchSettings.json b/src/TokenAuthExampleWebApplication/Properties/launchSettings.json index 3ea7047..2074ae4 100644 --- a/src/TokenAuthExampleWebApplication/Properties/launchSettings.json +++ b/src/TokenAuthExampleWebApplication/Properties/launchSettings.json @@ -17,7 +17,7 @@ "web": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:5000/api/values", + "launchUrl": "http://localhost:5000/", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/src/TokenAuthExampleWebApplication/RSAKeyUtils.cs b/src/TokenAuthExampleWebApplication/RSAKeyUtils.cs index beaac7b..caff3d1 100644 --- a/src/TokenAuthExampleWebApplication/RSAKeyUtils.cs +++ b/src/TokenAuthExampleWebApplication/RSAKeyUtils.cs @@ -1,10 +1,6 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.Cryptography; -using System.Threading.Tasks; namespace TokenAuthExampleWebApplication { diff --git a/src/TokenAuthExampleWebApplication/Startup.cs b/src/TokenAuthExampleWebApplication/Startup.cs index 1d0a33f..81fd5d9 100644 --- a/src/TokenAuthExampleWebApplication/Startup.cs +++ b/src/TokenAuthExampleWebApplication/Startup.cs @@ -63,10 +63,32 @@ public void ConfigureServices(IServiceCollection services) // Enable the use of an [Authorize("Bearer")] attribute on methods and classes to protect. services.AddAuthorization(auth => - { + { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​) .RequireAuthenticatedUser().Build()); + auth.DefaultPolicy = auth.GetPolicy("Bearer"); + }); + + // Note, it is VITAL that this is added BEFORE services.UseMvc() is called. + // See https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/issues/11 + services.AddJwtBearerAuthentication(o => + { + o.Audience = tokenOptions.Audience; + o.TokenValidationParameters = new TokenValidationParameters + { + IssuerSigningKey = key, + ValidAudience = tokenOptions.Audience, + ValidIssuer = tokenOptions.Issuer, + // When receiving a token, check that it is still valid. + ValidateLifetime = true, + + // This defines the maximum allowable clock skew - i.e. provides a tolerance on the token expiry time + // when validating the lifetime. As we're creating the tokens locally and validating them on the same + // machines which should have synchronised time, this can be set to zero. Where external tokens are + // used, some leeway here could be useful. + ClockSkew = TimeSpan.FromMinutes(0) + }; }); services.AddMvc(); @@ -113,31 +135,12 @@ await context.Response.WriteAsync( }); }); - // Note, it is VITAL that this is added BEFORE app.UseMvc() is called. - // See https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/issues/11 - app.UseJwtBearerAuthentication(new JwtBearerOptions { - TokenValidationParameters = new TokenValidationParameters - { - IssuerSigningKey = key, - ValidAudience = tokenOptions.Audience, - ValidIssuer = tokenOptions.Issuer, - - // When receiving a token, check that it is still valid. - ValidateLifetime = true, - - // This defines the maximum allowable clock skew - i.e. provides a tolerance on the token expiry time - // when validating the lifetime. As we're creating the tokens locally and validating them on the same - // machines which should have synchronised time, this can be set to zero. Where external tokens are - // used, some leeway here could be useful. - ClockSkew = TimeSpan.FromMinutes(0) - } - }); - // Configure the HTTP request pipeline. + app.UseDefaultFiles(); app.UseStaticFiles(); // Add MVC to the request pipeline. app.UseMvc(); } } -} +} \ No newline at end of file diff --git a/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj b/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj new file mode 100644 index 0000000..282a3e8 --- /dev/null +++ b/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp2.0 + aspnet-WebApplication2-25C1FC7A-1752-4DD6-97CC-4327790AF033 + + + + + + + + + + + \ No newline at end of file diff --git a/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.xproj b/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.xproj deleted file mode 100644 index f6f71ea..0000000 --- a/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.xproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - f6b6daa2-0460-419e-88d7-ce887777f469 - TokenAuthExampleWebApplication - .\obj - .\bin\ - - - 2.0 - 5092 - - - \ No newline at end of file diff --git a/src/TokenAuthExampleWebApplication/TokenAuthOptions.cs b/src/TokenAuthExampleWebApplication/TokenAuthOptions.cs index ff5c947..10331b8 100644 --- a/src/TokenAuthExampleWebApplication/TokenAuthOptions.cs +++ b/src/TokenAuthExampleWebApplication/TokenAuthOptions.cs @@ -1,9 +1,4 @@ using Microsoft.IdentityModel.Tokens; -using System; -using System.Collections.Generic; -using System.IdentityModel.Tokens; -using System.Linq; -using System.Threading.Tasks; namespace TokenAuthExampleWebApplication { diff --git a/src/TokenAuthExampleWebApplication/project.json b/src/TokenAuthExampleWebApplication/project.json deleted file mode 100644 index 9a5b971..0000000 --- a/src/TokenAuthExampleWebApplication/project.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "dependencies": { - "Microsoft.NETCore.App": { - "version": "1.0.1" - }, - "Microsoft.ApplicationInsights.AspNetCore": "1.0.0", - "Microsoft.AspNetCore.Mvc": "1.0.1", - "Microsoft.AspNetCore.Routing": "1.0.1", - "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", - "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", - "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", - "Microsoft.Extensions.Configuration.Json": "1.0.0", - "Microsoft.Extensions.Logging": "1.0.0", - "Microsoft.Extensions.Logging.Console": "1.0.0", - "Microsoft.Extensions.Logging.Debug": "1.0.0", - "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", - "Microsoft.IdentityModel.Tokens": "5.0.0", - "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0", - "Microsoft.AspNetCore.Diagnostics": "1.0.0", - "Microsoft.AspNetCore.StaticFiles": "1.0.0" - }, - - "tools": { - "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" - }, - - "frameworks": { - "netcoreapp1.0": { - "imports": [ - "dotnet5.6", - "portable-net45+win8" - ] - } - }, - - "runtimes": { - "win7-x64": {} - }, - - "buildOptions": { - "emitEntryPoint": true, - "preserveCompilationContext": true - }, - - "runtimeOptions": { - "configProperties": { - "System.GC.Server": true - } - }, - - "publishOptions": { - "include": [ - "wwwroot", - "**/*.cshtml", - "appsettings.json", - "web.config" - ] - }, - - "scripts": { - "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] - } -} diff --git a/test/client/javascript/index.html b/test/client/javascript/index.html new file mode 100644 index 0000000..4ce7508 --- /dev/null +++ b/test/client/javascript/index.html @@ -0,0 +1,88 @@ + + + + + + + +

Username

+ + +

Password

+ + +

+ + + + + +
+ + + + + \ No newline at end of file From ebde893ac1c86fb196c5eeab1f9ab2f870bdb968 Mon Sep 17 00:00:00 2001 From: "Jean.R" Date: Sun, 13 Aug 2017 22:55:54 +0200 Subject: [PATCH 2/4] Update fw version number --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 92857dc..ea6cb2d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ASP.NET Core Self-created token authentication example -A simple example of how to protect an ASP.NET Core Web API project using simple self-created JWT bearer tokens for local username/password checking. Working against dotnet core 1.0.1 as of 04/12/2016 - see the rc1, beta8 and beta7 branches if you're using older framework versions. +A simple example of how to protect an ASP.NET Core Web API project using simple self-created JWT bearer tokens for local username/password checking. Working against dotnet core 2.0 Preview 2 as of 10/08/2017 - see the 1.0.1, rc1, beta8 and beta7 branches if you're using older framework versions. **DO NOT USE AS-IS IN PRODUCTION** From f42cddb8c5932abd2db8d210c5e09f01cc876b11 Mon Sep 17 00:00:00 2001 From: "Jean.R" Date: Tue, 15 Aug 2017 23:20:13 +0200 Subject: [PATCH 3/4] Update to .Net Core 2.0 --- src/TokenAuthExampleWebApplication/Startup.cs | 2 +- .../TokenAuthExampleWebApplication.csproj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TokenAuthExampleWebApplication/Startup.cs b/src/TokenAuthExampleWebApplication/Startup.cs index 81fd5d9..3f8c033 100644 --- a/src/TokenAuthExampleWebApplication/Startup.cs +++ b/src/TokenAuthExampleWebApplication/Startup.cs @@ -72,7 +72,7 @@ public void ConfigureServices(IServiceCollection services) // Note, it is VITAL that this is added BEFORE services.UseMvc() is called. // See https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/issues/11 - services.AddJwtBearerAuthentication(o => + services.AddAuthentication().AddJwtBearer(o => { o.Audience = tokenOptions.Audience; o.TokenValidationParameters = new TokenValidationParameters diff --git a/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj b/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj index 282a3e8..504a77d 100644 --- a/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj +++ b/src/TokenAuthExampleWebApplication/TokenAuthExampleWebApplication.csproj @@ -6,11 +6,11 @@ - + - + \ No newline at end of file From a76bd1e1af2d824dd48c93c6493ed449bf3f65ea Mon Sep 17 00:00:00 2001 From: "Jean.R" Date: Tue, 15 Aug 2017 23:31:02 +0200 Subject: [PATCH 4/4] Replaced 2.0 preview 2 by 2.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ea6cb2d..f77dbe4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ASP.NET Core Self-created token authentication example -A simple example of how to protect an ASP.NET Core Web API project using simple self-created JWT bearer tokens for local username/password checking. Working against dotnet core 2.0 Preview 2 as of 10/08/2017 - see the 1.0.1, rc1, beta8 and beta7 branches if you're using older framework versions. +A simple example of how to protect an ASP.NET Core Web API project using simple self-created JWT bearer tokens for local username/password checking. Working against dotnet core 2.0 as of 14/08/2017 - see the 1.0.1, rc1, beta8 and beta7, 2.0 preview 2 branches if you're using older framework versions. **DO NOT USE AS-IS IN PRODUCTION**