Skip to content

Commit

Permalink
Merge pull request #24 from auth0-samples/netcore2.1
Browse files Browse the repository at this point in the history
Update samples to .NET Core 2.1
  • Loading branch information
jerriep authored Jun 10, 2018
2 parents e5c7303 + 684c854 commit 52daac6
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 117 deletions.
7 changes: 3 additions & 4 deletions Quickstart/00-Starter-Seed/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args)
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand All @@ -23,8 +23,7 @@ public static IWebHost BuildWebHost(string[] args)
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
}
}
}
4 changes: 3 additions & 1 deletion Quickstart/00-Starter-Seed/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -17,7 +18,8 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
11 changes: 3 additions & 8 deletions Quickstart/00-Starter-Seed/WebAPIApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>


</Project>
9 changes: 4 additions & 5 deletions Quickstart/01-Authorization/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args)
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand All @@ -23,8 +23,7 @@ public static IWebHost BuildWebHost(string[] args)
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
}
}
}
}
5 changes: 4 additions & 1 deletion Quickstart/01-Authorization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -25,7 +26,9 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
Expand Down
11 changes: 3 additions & 8 deletions Quickstart/01-Authorization/WebAPIApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>


</Project>
7 changes: 3 additions & 4 deletions Samples/hs256/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args)
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand All @@ -23,8 +23,7 @@ public static IWebHost BuildWebHost(string[] args)
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
}
}
}
4 changes: 3 additions & 1 deletion Samples/hs256/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -21,7 +22,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

string domain = $"https://{Configuration["Auth0:Domain"]}/";
string apiIdentifier = Configuration["Auth0:ApiIdentifier"];
Expand Down
11 changes: 3 additions & 8 deletions Samples/hs256/WebAPIApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>


</Project>
46 changes: 46 additions & 0 deletions Samples/multiple-issuer/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebAPIApplication.Controllers
{
[Route("api")]
public class ApiController : Controller
{
[HttpGet]
[Route("public")]
public IActionResult Public()
{
return Json(new
{
Message = "Hello from a public endpoint! You don't need to be authenticated to see this."
});
}

[HttpGet]
[Route("private")]
[Authorize]
public IActionResult Private()
{
return Json(new
{
Message = "Hello from a private endpoint! You need to be authenticated to see this."
});
}

/// <summary>
/// This is a helper action. It allows you to easily view all the claims of the token
/// </summary>
/// <returns></returns>
[HttpGet("claims")]
public IActionResult Claims()
{
return Json(User.Claims.Select(c =>
new
{
c.Type,
c.Value
}));
}
}
}
37 changes: 0 additions & 37 deletions Samples/multiple-issuer/Controllers/PingController.cs

This file was deleted.

11 changes: 3 additions & 8 deletions Samples/multiple-issuer/MultipleIssuer.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>


</Project>
12 changes: 3 additions & 9 deletions Samples/multiple-issuer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebAPIApplication
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args)
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand All @@ -28,8 +23,7 @@ public static IWebHost BuildWebHost(string[] args)
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
}
}
}
4 changes: 2 additions & 2 deletions Samples/multiple-issuer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Execute in command line `sh exec.sh` to run the Docker in Linux or macOS, or `.\

## Calling the API

Go to `http://localhost:3010/api/ping` in Postman (or your web browser) to access the ping API endpoint. To access the secure endpoint you will need to [obtain an access token](https://auth0.com/docs/tokens/access-token#how-to-get-an-access-token) and then pass the access token as a **Bearer** token in the **Authorization** header when calling the `http://localhost:3010/api/ping/secure` endpoint.
Go to `http://localhost:3010/api/public` in Postman (or your web browser) to access the ping API endpoint. To access the secure endpoint you will need to [obtain an access token](https://auth0.com/docs/tokens/access-token#how-to-get-an-access-token) and then pass the access token as a **Bearer** token in the **Authorization** header when calling the `http://localhost:3010/api/private` endpoint.

## Important Snippets

Expand All @@ -46,7 +46,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
{
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = "https://rs256.test.api",
ValidAudience = "https://quickstarts/api",
ValidIssuers = new List<string>(issuers),
IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => keyResolver.GetSigningKey(securityToken.Issuer, kid)
}
Expand Down
6 changes: 4 additions & 2 deletions Samples/multiple-issuer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -27,7 +28,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

string[] issuers = {
"https://jerrie.auth0.com/",
Expand All @@ -43,7 +45,7 @@ public void ConfigureServices(IServiceCollection services)
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = "https://rs256.test.api",
ValidAudience = "https://quickstarts/api",
ValidIssuers = new List<string>(issuers),
IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => keyResolver.GetSigningKey(securityToken.Issuer, kid)
};
Expand Down
Loading

0 comments on commit 52daac6

Please sign in to comment.