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
<PackageReference Include="Astrolabe.Web.Common" Version="1.3.1" />
<PackageVersion Include="Astrolabe.Web.Common" Version="1.3.1" />
<PackageReference Include="Astrolabe.Web.Common" />
paket add Astrolabe.Web.Common --version 1.3.1
#r "nuget: Astrolabe.Web.Common, 1.3.1"
#:package Astrolabe.Web.Common@1.3.1
#addin nuget:?package=Astrolabe.Web.Common&version=1.3.1
#tool nuget:?package=Astrolabe.Web.Common&version=1.3.1
Astrolabe.Web.Common
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 tokenIssuer
: The issuer of the tokenAudience
: 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 builderenv
: The web host environmentsiteDir
: The directory name containing the SPA's output files (relative toClientApp/sites
)domainPrefix
: The domain prefix to match (defaults tositeDir + "."
)fallback
: If true, the SPA will be used as a fallback for all requestsoptions
: Additional options for the SPA hostingmatch
: A custom function to match requestspathString
: 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.3)
- Microsoft.AspNetCore.SpaServices.Extensions (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.