Skip to content

Latest commit

 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔐 JWT Authorization Claims Middleware

CI .NET License: MIT

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 ClaimsPrincipal your authorization policies can rely on.


✨ What it does

Once a request has been authenticated (by JWT bearer auth, for example), the middleware:

  1. Reads the configured user-name claim from the incoming principal.
  2. Looks up the matching user in the security database (via a stored procedure).
  3. Adds Email, Name and IdUsuario claims.
  4. Looks up the user's profiles and adds a Role claim 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.


🧱 Architecture

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

📦 Installation

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.Middleware

🚀 Usage

1. 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;"
  }
}

⚙️ Configuration

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.

🧪 Building & testing

dotnet build Authorization.sln -c Release
dotnet test  Authorization.sln -c Release

CI runs on every push and pull request; packages are published from the main branch.


🛠️ Technologies

  • .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

📄 License

Released under the MIT License.

About

A simple and scalable middleware component for handling JWT-based authentication and authorization in .NET applications

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages