Astrolabe.Web.Common 1.3.1

dotnet add package Astrolabe.Web.Common --version 1.3.1
                    
NuGet\Install-Package Astrolabe.Web.Common -Version 1.3.1
                    
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="Astrolabe.Web.Common" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Astrolabe.Web.Common" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="Astrolabe.Web.Common" />
                    
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 Astrolabe.Web.Common --version 1.3.1
                    
#r "nuget: Astrolabe.Web.Common, 1.3.1"
                    
#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 Astrolabe.Web.Common@1.3.1
                    
#: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=Astrolabe.Web.Common&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=Astrolabe.Web.Common&version=1.3.1
                    
Install as a Cake Tool

Astrolabe.Web.Common

NuGet License: MIT

A utility library for .NET web projects that provides tools for JWT authentication and SPA virtual hosting.

Installation

dotnet add package Astrolabe.Web.Common

Features

  • JWT token generation and authentication
  • SPA virtual hosting with domain-based routing
  • Development mode controller filtering

JWT Authentication

BasicJwtToken

The BasicJwtToken class contains the essential parameters needed for JWT token generation and validation.

public record BasicJwtToken(byte[] SecretKey, string Issuer, string Audience);
Properties
  • SecretKey: The key used to sign the JWT token
  • Issuer: The issuer of the token
  • Audience: The intended audience of the token

Extension Methods

MakeTokenSigner

Creates a function that can generate JWT tokens with the specified claims and expiration.

public delegate string TokenGenerator(IEnumerable<Claim> claims, long expiresInSeconds);

// Usage
var tokenParams = new BasicJwtToken(secretKey, issuer, audience);
var tokenGenerator = tokenParams.MakeTokenSigner();
string token = tokenGenerator(claims, 3600); // Expires in 1 hour
ConfigureJwtBearer

Creates a configuration action for JWT bearer authentication.

// Usage in Startup.cs
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(jwtToken.ConfigureJwtBearer());

Virtual Hosting for SPAs

UseDomainSpa

The UseDomainSpa extension method allows you to host multiple SPAs in a single application, with routing based on domain prefixes or path segments.

public static IApplicationBuilder UseDomainSpa(
    this IApplicationBuilder app,
    IWebHostEnvironment env,
    string siteDir,
    string? domainPrefix = null,
    bool fallback = false,
    DomainSpaOptions? options = null,
    Func<HttpRequest, bool>? match = null,
    PathString? pathString = null
)
Parameters
  • app: The application builder
  • env: The web host environment
  • siteDir: The directory name containing the SPA's output files (relative to ClientApp/sites)
  • domainPrefix: The domain prefix to match (defaults to siteDir + ".")
  • fallback: If true, the SPA will be used as a fallback for all requests
  • options: Additional options for the SPA hosting
  • match: A custom function to match requests
  • pathString: A path string to match against instead of domain-based matching
Usage
// Match requests to admin.example.com
app.UseDomainSpa(env, "admin");

// Match requests to /dashboard
app.UseDomainSpa(
    env, 
    "dashboard", 
    domainPrefix: null, 
    pathString: "/dashboard"
);

// Custom matching function
app.UseDomainSpa(
    env, 
    "special", 
    match: req => req.Headers["X-Special"].Any()
);

DomainSpaOptions

Options for SPA hosting.

public class DomainSpaOptions
{
    public string CacheControl { get; set; } = "private, max-age=30, must-revalidate";
}
Properties
  • CacheControl: The Cache-Control header value for static files

HtmlFileProvider

The HtmlFileProvider is used internally to provide HTML files for the SPA. It has special handling for dynamic routes, where file paths with segments like [id] are matched against the request path.

Development Mode Features

DevModeAttribute

Mark controllers that should only be available in development mode.

[DevMode]
public class DevToolsController : Controller
{
    // This controller will be hidden in production
}

HideDevModeControllersConvention

A convention that removes controllers marked with DevModeAttribute from the application model.

Usage
// In Startup.cs
services.AddControllers(options =>
{
    if (!env.IsDevelopment())
    {
        options.Conventions.Add(new HideDevModeControllersConvention());
    }
});

Full Example

// Program.cs
using Astrolabe.Web.Common;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Setup JWT authentication
var jwtSecretKey = Encoding.UTF8.GetBytes(builder.Configuration["Jwt:SecretKey"]);
var jwtIssuer = builder.Configuration["Jwt:Issuer"];
var jwtAudience = builder.Configuration["Jwt:Audience"];
var jwtToken = new BasicJwtToken(jwtSecretKey, jwtIssuer, jwtAudience);

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(jwtToken.ConfigureJwtBearer());

// Add controllers but hide dev mode controllers in production
builder.Services.AddControllers(options =>
{
    if (!builder.Environment.IsDevelopment())
    {
        options.Conventions.Add(new HideDevModeControllersConvention());
    }
});

var app = builder.Build();

// Configure virtual hosting for SPAs
app.UseDomainSpa(app.Environment, "main", fallback: true);
app.UseDomainSpa(app.Environment, "admin");
app.UseDomainSpa(app.Environment, "dashboard", pathString: "/dashboard");

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

app.MapControllers();

app.Run();

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.3.1 296 5/22/2025
1.3.0 324 7/31/2024
1.2.0 717 5/14/2024
1.1.0 163 5/12/2024
1.0.0 184 4/8/2024