An ASP.NET Core middleware that enriches an already-authenticated request principal with identity claims (the user's id, name and email) and one role claim per profile, resolved from a SQL Server security store. It's shipped as a small set of NuGet packages so it can be dropped into any .NET 8 web app.
This is a personal side project. It sits between the HTTP request and your application logic, turning a bare authenticated token into a fully-populated
ClaimsPrincipalyour authorization policies can rely on.
Once a request has been authenticated (by JWT bearer auth, for example), the middleware:
- Reads the configured user-name claim from the incoming principal.
- Looks up the matching user in the security database (via a stored procedure).
- Adds
Email,NameandIdUsuarioclaims. - Looks up the user's profiles and adds a
Roleclaim for each one.
If anything goes wrong resolving that data (missing claim, unknown user, database outage), the request degrades gracefully: it continues unenriched instead of crashing the pipeline.
The solution is layered so each concern is isolated and independently testable:
| Project | Responsibility |
|---|---|
Authorization.Abstractions |
Contracts: entities, models, options and interfaces. No external dependencies. |
Authorization.Common |
Cached, reflection-based object mapper used to map entities → models. |
Authorization.DataAccess |
Dapper + Microsoft.Data.SqlClient access to stored procedures. |
Authorization.Business |
Thin business layer orchestrating identity resolution. |
Authorization.Middleware |
The ASP.NET Core middleware plus DI and pipeline extensions. |
Request ─▶ Authentication ─▶ ClaimsEnrichmentMiddleware ─▶ your app
│
IAuthorizationManager (Business)
│
ISecurityRepository (DataAccess)
│
IDbConnectionFactory ─▶ SQL Server
The packages are published to GitHub Packages. Add the feed and install the entry-point package (it pulls the rest in transitively):
dotnet nuget add source "https://nuget.pkg.github.com/Isma-L154/index.json" \
--name github --username <your-user> --password <your-PAT>
dotnet add package Authorization.Middleware1. Register the services (wires the connection factory, repository and business manager):
using Authorization.Middleware;
builder.Services.AddAuthorizationClaims();2. Add the middleware to the pipeline, after authentication:
app.UseAuthentication();
app.UseAuthorizationClaims(); // enrich the principal
app.UseAuthorization();3. Configure the connection string in appsettings.json:
{
"ConnectionStrings": {
"SecurityDb": "Server=...;Database=...;Trusted_Connection=True;Encrypt=True;"
}
}Everything that used to be hard-coded is now configurable through ClaimsEnrichmentOptions:
builder.Services.AddAuthorizationClaims(options =>
{
options.ConnectionStringName = "SecurityDb"; // ConnectionStrings key
options.UserNameClaimType = "usuario"; // inbound JWT claim to read
options.GetUserProcedure = "ObtenerUsuario"; // stored procedure names
options.GetProfilesProcedure = "ObtenerPerfilesxUsuario";
});| Option | Default | Description |
|---|---|---|
ConnectionStringName |
SecurityDb |
Key under ConnectionStrings for the security DB. |
UserNameClaimType |
usuario |
Inbound claim type carrying the user name. |
GetUserProcedure |
ObtenerUsuario |
Stored procedure returning a user by name/email. |
GetProfilesProcedure |
ObtenerPerfilesxUsuario |
Stored procedure returning a user's profiles. |
dotnet build Authorization.sln -c Release
dotnet test Authorization.sln -c ReleaseCI runs on every push and pull request; packages are published from the main branch.
- .NET 8.0, C# latest
- Dapper for micro-ORM data access
- Microsoft.Data.SqlClient (the maintained SQL Server driver)
- xUnit + Moq for unit tests
- Distributed as NuGet packages via GitHub Packages
Released under the MIT License.