-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrates interactive application with api usage
- Loading branch information
1 parent
982ac0a
commit f11fcab
Showing
7 changed files
with
163 additions
and
58 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
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 |
---|---|---|
@@ -1,60 +1,89 @@ | ||
using Serilog; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateBootstrapLogger(); | ||
|
||
// Add services to the container. | ||
builder.Services.AddRazorPages(); | ||
try | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false; | ||
// Add services to the container. | ||
builder.Host.UseSerilog((context, services, configuration) => configuration | ||
.ReadFrom.Configuration(context.Configuration) | ||
.ReadFrom.Services(services) | ||
.Enrich.FromLogContext()); | ||
|
||
builder.Services.AddAuthentication(options => | ||
{ | ||
options.DefaultScheme = "cookie"; | ||
options.DefaultChallengeScheme = "oidc"; | ||
}) | ||
.AddCookie("cookie", options => | ||
{ | ||
options.Cookie.HttpOnly = true; | ||
options.Cookie.SameSite = SameSiteMode.Strict; | ||
} | ||
) | ||
.AddOpenIdConnect("oidc", options => | ||
{ | ||
options.Authority = "https://localhost:5001"; | ||
builder.Services.AddRazorPages(); | ||
|
||
options.ClientId = "web"; | ||
options.ClientSecret = "secret"; | ||
options.ResponseType = "code"; | ||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false; | ||
|
||
options.Scope.Clear(); | ||
options.Scope.Add("openid"); | ||
options.Scope.Add("email"); | ||
options.Scope.Add("profile"); | ||
options.Scope.Add("api1"); | ||
options.Scope.Add("offline_access"); | ||
options.GetClaimsFromUserInfoEndpoint = true; | ||
builder.Services | ||
.AddAuthentication(options => | ||
{ | ||
options.DefaultAuthenticateScheme = "cookie"; | ||
options.DefaultSignInScheme = "cookie"; | ||
options.DefaultChallengeScheme = "oidc"; | ||
}) | ||
.AddCookie("cookie", options => | ||
{ | ||
options.Cookie.HttpOnly = true; | ||
// options.Cookie.SameSite = SameSiteMode.Strict; | ||
} | ||
) | ||
.AddOpenIdConnect("oidc", options => | ||
{ | ||
options.Authority = "https://localhost:5001"; | ||
options.Resource = "urn:calcapi"; | ||
|
||
options.SaveTokens = true; | ||
}); | ||
options.ClientId = "web"; | ||
options.ClientSecret = "secret"; | ||
options.ResponseType = "code"; | ||
|
||
var app = builder.Build(); | ||
options.Scope.Clear(); | ||
options.Scope.Add("openid"); | ||
options.Scope.Add("email"); | ||
options.Scope.Add("profile"); | ||
options.Scope.Add("calc:double"); | ||
options.Scope.Add("offline_access"); | ||
options.GetClaimsFromUserInfoEndpoint = true; | ||
|
||
// Configure the HTTP request pipeline. | ||
if (!app.Environment.IsDevelopment()) | ||
{ | ||
app.UseExceptionHandler("/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
options.SaveTokens = true; | ||
}); | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
var app = builder.Build(); | ||
|
||
app.UseRouting(); | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
// Configure Serilog request logging. | ||
app.UseSerilogRequestLogging(); | ||
|
||
app.MapRazorPages().RequireAuthorization(); | ||
// Configure the HTTP request pipeline. | ||
if (!app.Environment.IsDevelopment()) | ||
{ | ||
app.UseExceptionHandler("/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.Run(); | ||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapRazorPages().RequireAuthorization(); | ||
|
||
app.Run(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "An unhandled exception occured during bootstrapping"); | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.12" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.2" /> | ||
<PackageReference Include="Serilog" Version="3.1.1" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" /> | ||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" /> | ||
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" /> | ||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> | ||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
</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,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.002.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebClient", "WebClient.csproj", "{C0EBEAFD-8992-47A0-881F-BFC1AE9A748C}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{C0EBEAFD-8992-47A0-881F-BFC1AE9A748C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C0EBEAFD-8992-47A0-881F-BFC1AE9A748C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C0EBEAFD-8992-47A0-881F-BFC1AE9A748C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C0EBEAFD-8992-47A0-881F-BFC1AE9A748C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {A3440DD1-A87E-47C7-966B-DC51D06FB0AF} | ||
EndGlobalSection | ||
EndGlobal |
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 |
---|---|---|
@@ -1,9 +1,24 @@ | ||
{ | ||
"DetailedErrors": true, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
"Serilog": { | ||
"MinimumLevel": { | ||
"Default": "Debug", | ||
"Override": { | ||
"Azure": "Information", | ||
"IdentityModel": "Warning", | ||
"Duende": "Warning", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information", | ||
"System": "Information" | ||
} | ||
}, | ||
"Properties": { | ||
"ApplicationName": "WebClient" | ||
}, | ||
"WriteTo": [ | ||
{ | ||
"Name": "Console" | ||
} | ||
] | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,31 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Serilog": { | ||
"MinimumLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
"Override": { | ||
"Azure": "Information", | ||
"IdentityModel": "Warning", | ||
"Duende": "Warning", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information", | ||
"Microsoft.AspNetCore.Authentication": "Debug", | ||
"System": "Warning" | ||
} | ||
}, | ||
"Enrich": [ | ||
"FromLogContext", | ||
"WithMachineName", | ||
"withProcessId", | ||
"WithThreadId" | ||
], | ||
"Properties": { | ||
"ApplicationName": "WebClient" | ||
}, | ||
"WriteTo": [ | ||
{ | ||
"Name": "Console" | ||
} | ||
] | ||
}, | ||
"AllowedHosts": "*" | ||
} |