This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66e8418
commit a22a3eb
Showing
59 changed files
with
39,856 additions
and
4 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
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,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"); | ||
} | ||
} | ||
} |
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,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>(); | ||
}); | ||
} | ||
} |
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,12 @@ | ||
{ | ||
"profiles": { | ||
"MvcCode": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,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(); | ||
}); | ||
} | ||
} | ||
} |
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,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> |
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,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) | ||
{ | ||
} | ||
} | ||
} |
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,3 @@ | ||
<h1>API Response</h1> | ||
|
||
<pre>@ViewBag.Json</pre> |
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,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> |
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,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> |
Oops, something went wrong.