Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
add net5 samples
Browse files Browse the repository at this point in the history
  • Loading branch information
leastprivilege committed Nov 23, 2020
1 parent 66e8418 commit a22a3eb
Show file tree
Hide file tree
Showing 59 changed files with 39,856 additions and 4 deletions.
32 changes: 31 additions & 1 deletion IdentityModel.AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{A7311E40-E
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "test\Tests\Tests.csproj", "{A891D28E-52E0-4B93-BE47-92DE2B9CB655}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "samples\WorkerService\WorkerService.csproj", "{F44432B9-0846-4A2A-80B8-6B121A1D8C38}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService3", "samples\WorkerService3\WorkerService3.csproj", "{F44432B9-0846-4A2A-80B8-6B121A1D8C38}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService5", "samples\WorkerService5\WorkerService5.csproj", "{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TokenManagement5", "samples\TokenManagement5\TokenManagement5.csproj", "{0F751A6E-EFB1-483E-BDF5-008644C07C07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -75,6 +79,30 @@ Global
{F44432B9-0846-4A2A-80B8-6B121A1D8C38}.Release|x64.Build.0 = Release|Any CPU
{F44432B9-0846-4A2A-80B8-6B121A1D8C38}.Release|x86.ActiveCfg = Release|Any CPU
{F44432B9-0846-4A2A-80B8-6B121A1D8C38}.Release|x86.Build.0 = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|x64.ActiveCfg = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|x64.Build.0 = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|x86.ActiveCfg = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Debug|x86.Build.0 = Debug|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|Any CPU.Build.0 = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|x64.ActiveCfg = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|x64.Build.0 = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|x86.ActiveCfg = Release|Any CPU
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC}.Release|x86.Build.0 = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|x64.ActiveCfg = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|x64.Build.0 = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|x86.ActiveCfg = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Debug|x86.Build.0 = Debug|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|Any CPU.Build.0 = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|x64.ActiveCfg = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|x64.Build.0 = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|x86.ActiveCfg = Release|Any CPU
{0F751A6E-EFB1-483E-BDF5-008644C07C07}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -84,6 +112,8 @@ Global
{7BF76B2C-3D3B-4B54-A7A4-9311E959BEDE} = {B8F1A24B-007A-4735-B33E-9528662EDCAD}
{A891D28E-52E0-4B93-BE47-92DE2B9CB655} = {A7311E40-E3DC-4FE8-906C-FC943B7E7444}
{F44432B9-0846-4A2A-80B8-6B121A1D8C38} = {B8F1A24B-007A-4735-B33E-9528662EDCAD}
{2F0D8378-1ADA-4F0B-B8E6-7E1D97F1CBCC} = {B8F1A24B-007A-4735-B33E-9528662EDCAD}
{0F751A6E-EFB1-483E-BDF5-008644C07C07} = {B8F1A24B-007A-4735-B33E-9528662EDCAD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7A4E29A0-CAE8-45E9-817B-AF126F85EE15}
Expand Down
63 changes: 63 additions & 0 deletions samples/TokenManagement5/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace MvcCode.Controllers
{
public class HomeController : Controller
{
private readonly IHttpClientFactory _httpClientFactory;

public HomeController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

[AllowAnonymous]
public IActionResult Index() => View();

public IActionResult Secure() => View();

public IActionResult Logout() => SignOut("cookie", "oidc");

public async Task<IActionResult> CallApiAsUser()
{
var client = _httpClientFactory.CreateClient("user_client");

var response = await client.GetStringAsync("test");
ViewBag.Json = JArray.Parse(response).ToString();

return View("CallApi");
}

public async Task<IActionResult> CallApiAsUserTyped([FromServices] TypedUserClient client)
{
var response = await client.CallApi();
ViewBag.Json = JArray.Parse(response).ToString();

return View("CallApi");
}

[AllowAnonymous]
public async Task<IActionResult> CallApiAsClient()
{
var client = _httpClientFactory.CreateClient("client");

var response = await client.GetStringAsync("test");
ViewBag.Json = JArray.Parse(response).ToString();

return View("CallApi");
}

[AllowAnonymous]
public async Task<IActionResult> CallApiAsClientTyped([FromServices] TypedClientClient client)
{
var response = await client.CallApi();
ViewBag.Json = JArray.Parse(response).ToString();

return View("CallApi");
}
}
}
33 changes: 33 additions & 0 deletions samples/TokenManagement5/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace MvcCode
{
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("System", LogEventLevel.Error)
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.MinimumLevel.Override("System.Net.Http", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();

CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
12 changes: 12 additions & 0 deletions samples/TokenManagement5/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"MvcCode": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
125 changes: 125 additions & 0 deletions samples/TokenManagement5/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Polly;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;

namespace MvcCode
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

services.AddControllersWithViews();

services.AddAuthentication(options =>
{
options.DefaultScheme = "cookie";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookie", options =>
{
options.Cookie.Name = "mvccode";
options.Events.OnSigningOut = async e =>
{
await e.HttpContext.RevokeUserRefreshTokenAsync();
};
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://demo.identityserver.io";
options.ClientId = "interactive.confidential.short";
options.ClientSecret = "secret";
// code flow + PKCE (PKCE is turned on by default)
options.ResponseType = "code";
options.UsePkce = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("offline_access");
options.Scope.Add("api");
// not mapped by default
options.ClaimActions.MapJsonKey("website", "website");
// keeps id_token smaller
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});

// adds user and client access token management
services.AddAccessTokenManagement(options =>
{
// client config is inferred from OpenID Connect settings
// if you want to specify scopes explicitly, do it here, otherwise the scope parameter will not be sent
options.Client.Scope = "api";
})
.ConfigureBackchannelHttpClient()
.AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
}));

// registers HTTP client that uses the managed user access token
services.AddUserAccessTokenClient("user_client", client =>
{
client.BaseAddress = new Uri("https://demo.identityserver.io/api/");
});

// registers HTTP client that uses the managed client access token
services.AddClientAccessTokenClient("client", configureClient: client =>
{
client.BaseAddress = new Uri("https://demo.identityserver.io/api/");
});

// registers a typed HTTP client with token management support
services.AddHttpClient<TypedUserClient>(client =>
{
client.BaseAddress = new Uri("https://demo.identityserver.io/api/");
})
.AddUserAccessTokenHandler();

services.AddHttpClient<TypedClientClient>(client =>
{
client.BaseAddress = new Uri("https://demo.identityserver.io/api/");
})
.AddClientAccessTokenHandler();
}

public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
});
}
}
}
19 changes: 19 additions & 0 deletions samples/TokenManagement5/TokenManagement5.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.0" />

<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\IdentityModel.AspNetCore.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions samples/TokenManagement5/TypedClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace MvcCode
{
public abstract class TypedClient
{
private readonly HttpClient _client;

public TypedClient(HttpClient client)
{
_client = client;
}

public virtual async Task<string> CallApi()
{
return await _client.GetStringAsync("test");
}
}

public class TypedUserClient : TypedClient
{
public TypedUserClient(HttpClient client) : base(client)
{
}
}

public class TypedClientClient : TypedClient
{
public TypedClientClient(HttpClient client) : base(client)
{
}
}
}
3 changes: 3 additions & 0 deletions samples/TokenManagement5/Views/Home/CallApi.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>API Response</h1>

<pre>@ViewBag.Json</pre>
8 changes: 8 additions & 0 deletions samples/TokenManagement5/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
21 changes: 21 additions & 0 deletions samples/TokenManagement5/Views/Home/Secure.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@using Microsoft.AspNetCore.Authentication

<h2>Claims</h2>

<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
</dl>

<h2>Properties</h2>

<dl>
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>@prop.Key</dt>
<dd>@prop.Value</dd>
}
</dl>
Loading

0 comments on commit a22a3eb

Please sign in to comment.