Skip to content

Commit

Permalink
Updated web client
Browse files Browse the repository at this point in the history
Demonstrates interactive application with api usage
  • Loading branch information
danielwagn3r committed Feb 15, 2024
1 parent 982ac0a commit f11fcab
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 58 deletions.
9 changes: 8 additions & 1 deletion src/WebClient/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
## Get latest from `dotnet new gitignore`

# dotenv files
.env

# User-specific files
*.rsuser
Expand Down Expand Up @@ -399,6 +402,7 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
.idea

##
## Visual studio for Mac
Expand Down Expand Up @@ -475,3 +479,6 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# Vim temporary swap files
*.swp
2 changes: 1 addition & 1 deletion src/WebClient/Pages/CallApi.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task OnGet()
var accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = await client.GetStringAsync("https://localhost:6001/identity");
var content = await client.GetStringAsync("https://localhost:6001/Double/4");

var parsed = JsonDocument.Parse(content);
var formatted = JsonSerializer.Serialize(parsed, new JsonSerializerOptions { WriteIndented = true });
Expand Down
119 changes: 74 additions & 45 deletions src/WebClient/Program.cs
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();
}
11 changes: 9 additions & 2 deletions src/WebClient/WebClient.csproj
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>
25 changes: 25 additions & 0 deletions src/WebClient/WebClient.sln
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
25 changes: 20 additions & 5 deletions src/WebClient/appsettings.Development.json
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"
}
]
}
}
30 changes: 26 additions & 4 deletions src/WebClient/appsettings.json
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": "*"
}

0 comments on commit f11fcab

Please sign in to comment.