PolyhydraGames.Core.AspNet 2.0.0.17

dotnet add package PolyhydraGames.Core.AspNet --version 2.0.0.17
                    
NuGet\Install-Package PolyhydraGames.Core.AspNet -Version 2.0.0.17
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PolyhydraGames.Core.AspNet" Version="2.0.0.17" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PolyhydraGames.Core.AspNet" Version="2.0.0.17" />
                    
Directory.Packages.props
<PackageReference Include="PolyhydraGames.Core.AspNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PolyhydraGames.Core.AspNet --version 2.0.0.17
                    
#r "nuget: PolyhydraGames.Core.AspNet, 2.0.0.17"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PolyhydraGames.Core.AspNet@2.0.0.17
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PolyhydraGames.Core.AspNet&version=2.0.0.17
                    
Install as a Cake Addin
#tool nuget:?package=PolyhydraGames.Core.AspNet&version=2.0.0.17
                    
Install as a Cake Tool

Core.AspNet

Shared ASP.NET Core helpers used across PolyhydraGames services and clients. The package focuses on common identity, logging, and controller building blocks so you can keep app code small and consistent.

Tags

  • dotnet
  • core
  • core-asp-net
  • docs
  • testing
  • devops

Features

  • Base API controllers for DTO and named DTO read endpoints
  • Identity services for API, Blazor Server, and client-side apps
  • OpenId Connect refresh token support
  • Owner ID middleware for propagating authenticated user ID
  • Jwt and HttpContext helpers
  • Logging registration with Seq support

Requirements

  • .NET SDK 10.0 (targets net10.0)
  • ASP.NET Core authentication packages (transitive via NuGet)

Installation

dotnet add package PolyhydraGames.Core.AspNet

Quick Start

Register logging

var builder = WebApplication.CreateBuilder(args);
builder.AddLogs();

Add owner middleware

var app = builder.Build();
app.UseAuthentication();
app.UseOwnerMiddleware();

Add refresh middleware

var app = builder.Build();
app.UseAuthentication();
app.UseRefreshMiddleware();

Use base controllers

public class WidgetsController : DtoBaseController<WidgetDto>
{
    public WidgetsController(IReadByIdServiceAsync<WidgetDto> repo) : base(repo) { }
}

Configure identity services

// API
services.AddScoped<IIdentityService, ApiIdentityService>();

// Blazor Server
services.AddScoped<IIdentityService, ServerSideIdentityService>();

// Client-side (OidcClient provided by your app)
services.AddScoped<IAuthenticationClient, ClientSideIdentityService>();

Configuration

Refresh middleware

Refresh middleware expects standard OIDC configuration keys:

OIDC:Authority
OIDC:ClientId
OIDC:ClientSecret

The middleware uses OIDC discovery to resolve the token endpoint and stores refreshed tokens in authentication properties so GetTokenAsync continues to work.

Token expiration

Token expiry checks use the standard JWT exp claim via JwtSecurityToken.ValidTo. If the access token is missing or empty, it is treated as expired.

API Notes

Controllers

  • DtoBaseController<T> exposes Items, Page, Count, Any, and ItemById.
  • NamedDtoBaseController<T> adds name-based queries like ItemsByName and ItemByName.
  • Use IReadByIdServiceAsync<T> for DTO controllers and INamedReadServiceAsync<T> for named DTO controllers to enforce compile-time contracts.

Identity services

  • ApiIdentityService is for API hosts where owner ID is provided in tokens.
  • ServerSideIdentityService listens to AuthenticationStateProvider in Blazor Server.
  • ClientSideIdentityService wraps OidcClient for interactive login flows.
  • AuthenticationStateExtensions.ToPrincipalContext() and AuthenticationStateProvider.GetCurrentPrincipalContextAsync() provide a reusable authenticated-principal snapshot containing the source principal, authentication status, owner ID, and best-effort display username. Use these helpers when app-level authorization needs principal claims before local app-user records have hydrated.

Middleware

  • OwnerMiddleware sets IIdentityService.OwnerId and AuthorizationToken.
  • RefreshMiddleware attempts a refresh token flow when JWTs are expired.

Build and Test

dotnet build
dotnet test

Sample Host (Middleware Testing)

A minimal sample app is available at samples/CoreAspNet.SampleHost/ to exercise UseOwnerMiddleware() and UseRefreshMiddleware().

Quick test flow

# from repo root
cd /home/lancer1977/code/Core.AspNet

# run the sample host
dotnet run --project samples/CoreAspNet.SampleHost

In another terminal:

curl http://localhost:5000/debug/identity

Expected result: JSON containing OwnerId, HasAccessToken, and token metadata populated by the sample auth handler + middleware.

appsettings.Development.json in the sample includes placeholder OIDC:* keys:

  • OIDC:Authority
  • OIDC:ClientId
  • OIDC:ClientSecret

Contributing

  • Keep public APIs documented with XML comments.
  • Add tests for new middleware or identity behaviors.
  • Follow existing naming and namespace conventions.

CI/CD Configuration

GitHub Actions handles build, test, pack, and publish.

Workflows

  • ci.yml builds and tests on pushes and pull requests.
  • publish.yml validates, packs, and publishes to NuGet.org only for version tags (v<PackageVersion>) or an explicit manual dispatch. It never publishes on a branch push.

Package Publishing

NuGet.org is the canonical package host. The currently published source version is 1.1.0.1; update <Version> in src/PolyhydraGames.Core.AspNet.csproj before creating a future v<PackageVersion> release tag. The package workflow verifies the package identity, version, and README inclusion before release.

📖 Documentation

Detailed documentation can be found in the following sections:

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0.17 65 10/14/2026
1.1.0.1 5,205 7/9/2026
1.0.2.48 2,927 6/7/2026
1.0.2.47 128 6/7/2026
1.0.2.46 136 6/7/2026
1.0.2.45 209 5/29/2026
1.0.2.44 156 5/23/2026
1.0.2.43 526 4/7/2026
1.0.2.42 186 4/4/2026
1.0.2.41 199 3/30/2026
1.0.2.38 202 3/23/2026
1.0.2.36 280 2/14/2026
1.0.2.34 193 1/29/2026
1.0.2.32 214 1/26/2026
1.0.2.31 157 1/26/2026
1.0.2.30 162 1/26/2026
1.0.2.28 553 12/11/2025
1.0.2.27 276 12/5/2025
1.0.2.26 438 11/30/2025
1.0.2.25 350 11/16/2025
Loading failed