HoneyDrunk.Auth.AspNetCore
0.2.0
dotnet add package HoneyDrunk.Auth.AspNetCore --version 0.2.0
NuGet\Install-Package HoneyDrunk.Auth.AspNetCore -Version 0.2.0
<PackageReference Include="HoneyDrunk.Auth.AspNetCore" Version="0.2.0" />
<PackageVersion Include="HoneyDrunk.Auth.AspNetCore" Version="0.2.0" />
<PackageReference Include="HoneyDrunk.Auth.AspNetCore" />
paket add HoneyDrunk.Auth.AspNetCore --version 0.2.0
#r "nuget: HoneyDrunk.Auth.AspNetCore, 0.2.0"
#:package HoneyDrunk.Auth.AspNetCore@0.2.0
#addin nuget:?package=HoneyDrunk.Auth.AspNetCore&version=0.2.0
#tool nuget:?package=HoneyDrunk.Auth.AspNetCore&version=0.2.0
HoneyDrunk.Auth.AspNetCore
ASP.NET Core integration for HoneyDrunk.Auth - Middleware, identity accessors, and authorization endpoint helpers.
🌐 What Is This?
This package provides middleware and extensions for integrating HoneyDrunk.Auth with ASP.NET Core applications. It includes authentication middleware that automatically validates Bearer tokens, HttpContext-based identity accessors, and convenient authorization helpers for endpoints.
📦 Installation
dotnet add package HoneyDrunk.Auth.AspNetCore
<PackageReference Include="HoneyDrunk.Auth.AspNetCore" Version="0.1.0" />
🚀 Quick Start
1. Register Services
builder.Services.AddHoneyDrunkAuthAspNetCore();
2. Configure Middleware
app.UseGridContext(); // Grid context propagation
app.UseHoneyDrunkAuth(); // Authentication middleware
3. Access Identity
app.MapGet("/profile", (IAuthenticatedIdentityAccessor identity) =>
{
if (!identity.IsAuthenticated)
return Results.Unauthorized();
return Results.Ok(new
{
identity.Identity!.SubjectId,
identity.Identity.DisplayName,
Roles = identity.Identity.GetClaimValues(AuthClaimTypes.Role)
});
});
4. Authorize Requests
app.MapPost("/admin/action", async (HttpContext ctx) =>
{
var request = new AuthorizationRequest(
action: "admin",
resource: "system",
requiredRoles: ["admin"]);
if (!await ctx.AuthorizeOrForbidAsync(request))
return Results.Empty; // 403 already sent
// Authorized - perform action
return Results.Ok();
});
🔧 Key Components
Middleware
| Component | Description |
|---|---|
HoneyDrunkAuthMiddleware |
Validates Bearer tokens and sets identity on HttpContext |
UseHoneyDrunkAuth() |
Extension method to add middleware to pipeline |
Identity Access
| Component | Description |
|---|---|
IAuthenticatedIdentityAccessor |
Interface to access current authenticated identity |
HttpContextIdentityAccessor |
HttpContext-based implementation |
Authorization Helpers
| Method | Description |
|---|---|
AuthorizeAsync() |
Evaluates authorization, returns decision |
AuthorizeOrForbidAsync() |
Evaluates authorization, sends 403 if denied |
RequireAuthentication() |
Returns 401 if not authenticated |
💡 Usage Examples
Check Authentication
app.MapGet("/me", (IAuthenticatedIdentityAccessor identity) =>
{
if (!identity.IsAuthenticated)
return Results.Unauthorized();
return Results.Ok(new { identity.Identity!.SubjectId });
});
Role-Based Authorization
app.MapDelete("/users/{id}", async (HttpContext ctx, string id) =>
{
var request = new AuthorizationRequest(
action: "delete",
resource: $"users/{id}",
requiredRoles: ["admin"]);
if (!await ctx.AuthorizeOrForbidAsync(request))
return Results.Empty;
// Delete user...
return Results.NoContent();
});
Scope-Based Authorization
app.MapPost("/documents", async (HttpContext ctx) =>
{
var request = new AuthorizationRequest(
action: "create",
resource: "documents",
requiredScopes: ["documents:write"]);
var decision = await ctx.AuthorizeAsync(request);
if (!decision.IsAllowed)
{
return Results.Json(new
{
error = "Forbidden",
reasons = decision.DenyReasons.Select(r => r.Message)
}, statusCode: 403);
}
// Create document...
return Results.Created("/documents/123", new { id = "123" });
});
Simple Authentication Check
app.MapGet("/protected", (HttpContext ctx) =>
{
if (!ctx.RequireAuthentication())
return Results.Empty; // 401 already sent
return Results.Ok("You are authenticated!");
});
🔀 Middleware Pipeline
The middleware should be placed after UseGridContext() for proper context propagation:
var app = builder.Build();
app.UseRouting(); // Route matching (if using controllers)
app.UseGridContext(); // Grid context propagation
app.UseHoneyDrunkAuth(); // Authentication middleware
app.UseAuthorization(); // Optional: ASP.NET Core authorization
app.MapControllers();
app.Run();
📋 What Gets Registered
When you call AddHoneyDrunkAuthAspNetCore():
| Service | Implementation | Lifetime |
|---|---|---|
ISigningKeyProvider |
VaultSigningKeyProvider |
Singleton |
IAuthenticationProvider |
BearerTokenAuthenticationProvider |
Singleton |
IAuthorizationPolicy |
DefaultAuthorizationPolicy |
Singleton |
IStartupHook |
AuthStartupHook |
Singleton |
IHealthContributor |
AuthHealthContributor |
Singleton |
IReadinessContributor |
AuthReadinessContributor |
Singleton |
IHttpContextAccessor |
HttpContextAccessor |
Singleton |
IAuthenticatedIdentityAccessor |
HttpContextIdentityAccessor |
Singleton |
📚 Dependencies
| Package | Purpose |
|---|---|
HoneyDrunk.Auth |
Core authentication runtime |
HoneyDrunk.Auth.Abstractions |
Core contracts |
HoneyDrunk.Kernel |
Grid context and telemetry |
🔗 Related Packages
| Package | Description |
|---|---|
| HoneyDrunk.Auth.Abstractions | Core contracts (no dependencies) |
| HoneyDrunk.Auth | Core runtime with JWT validation |
📖 Documentation
- AspNetCore Guide - Detailed middleware and extension documentation
- DependencyInjection Guide - Service registration
- FILE_GUIDE.md - Complete architecture reference
⚖️ License
This project is licensed under the MIT License.
<div align="center">
Built with ❤️ by HoneyDrunk Studios
</div>
| Product | Versions 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. |
-
net10.0
- HoneyDrunk.Auth (>= 0.2.0)
- HoneyDrunk.Auth.Abstractions (>= 0.2.0)
- HoneyDrunk.Kernel (>= 0.4.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on HoneyDrunk.Auth.AspNetCore:
| Package | Downloads |
|---|---|
|
HoneyDrunk.Web.Rest.AspNetCore
ASP.NET Core integration for HoneyDrunk REST conventions. Provides middleware for correlation propagation, exception mapping, and request logging. Includes MVC filters for model validation, minimal API endpoint conventions, and JSON serialization defaults. Ensures consistent API responses across all services. |
GitHub repositories
This package is not used by any popular GitHub repositories.
v0.2.0: Updated HoneyDrunk.Kernel to 0.4.0 for scope-based GridContext support. Fixed XML doc cref on AddHoneyDrunkAuthAspNetCore.