RePolicies.AspNetCore 1.0.0

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

RePolicies.AspNetCore

ASP.NET Core integration for RePolicies.

This package builds a Policy<TModel> envelope from the current HttpContext and lets you enforce the configured policies three ways: manually in a handler, as an authorization (endpoint) filter, or as global middleware. All three produce the same denial response (403 by default).

The policy model, engine and runtime reconfiguration live in the core RePolicies package — start there for how policies are defined and reconfigured. This package adds the HTTP layer.


Install

dotnet add package RePolicies.AspNetCore

Setup

using RePolicies.AspNetCore;
using RePolicies.DependencyInjection;

builder.Services.AddRePolicies();      // core engine (calls AddReValidator())
builder.Services.AddRePoliciesHttp();  // IPolicyFactory + enforcement options

builder.Services.AddPolicy<CreateOrderRequest>(new PolicyDefinition
{
    Name = "TenantHeaderRequired",
    Expression = "x => x.HasHeader(\"X-Tenant-Id\")",
    DenialReason = "The X-Tenant-Id header is required."
});

AddRePoliciesHttp() registers IPolicyFactory (the HttpContextPolicy<TModel> builder) and the shared PolicyEnforcementOptions.


Enforcing policies

1. Manual evaluation

Most control. Resolve IPolicyFactory + IPolicyEngine and decide what to return:

app.MapPost("/orders", (CreateOrderRequest req, HttpContext ctx,
                        IPolicyEngine engine, IPolicyFactory factory) =>
{
    var policy = factory.Create(req, ctx);   // wraps body + request context
    var result = engine.Evaluate(policy);

    return result.IsSatisfied
        ? Results.Ok()
        : Results.Json(new { result.Failures }, statusCode: 403);
});

2. As an authorization (endpoint) filter

RequirePolicies<TModel>() attaches PolicyEndpointFilter<TModel>, which evaluates the policies after model binding and short-circuits with the denial response — the handler only runs for allowed requests:

app.MapPost("/orders", (CreateOrderRequest req) => Results.Ok())
   .RequirePolicies<CreateOrderRequest>();

// Also available on route groups:
var orders = app.MapGroup("/orders").RequirePolicies<CreateOrderRequest>();

3. As global middleware

UseRePolicies() enforces policies centrally, before the endpoint executes. It only acts on endpoints marked with WithPolicyModel<TModel>() (everything else passes through), reading and rewinding the request body so normal model binding still works:

app.UseRePolicies();   // after routing, before endpoint execution

app.MapPost("/orders", (CreateOrderRequest req) => Results.Ok())
   .WithPolicyModel<CreateOrderRequest>();

Pick one mechanism per endpoint — attaching both simply evaluates twice.


Customising the denial response

Configure PolicyEnforcementOptions (used by both the filter and the middleware) via AddRePoliciesHttp:

builder.Services.AddRePoliciesHttp(options =>
{
    options.DeniedStatusCode = StatusCodes.Status403Forbidden;
    options.ResponseBodyFactory = result => new { errors = result.Failures };
    // options.JsonSerializerOptions = ...
});

The default body is { "denied": [ { "policyName": ..., "reasons": [...] } ] }.


Key types

Type Role
IPolicyFactory / HttpContextPolicyFactory Builds Policy<TModel> from an HttpContext.
PolicyEndpointFilter<TModel> IEndpointFilter that enforces a model's policies.
PolicyEnforcementMiddleware Global enforcement for endpoints marked with metadata.
PolicyEnforcementMetadata Endpoint metadata attached by WithPolicyModel<TModel>().
PolicyEnforcementOptions Denial status code, JSON options and response shape.

Extension methods

Method Target Effect
AddRePoliciesHttp(...) IServiceCollection Registers the factory and options.
RequirePolicies<TModel>() RouteHandlerBuilder / RouteGroupBuilder Adds the endpoint filter.
WithPolicyModel<TModel>() RouteHandlerBuilder / RouteGroupBuilder Marks the endpoint for the middleware.
UseRePolicies() IApplicationBuilder Adds the enforcement middleware.

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.0.0 119 6/24/2026