RePolicies.AspNetCore
1.0.0
dotnet add package RePolicies.AspNetCore --version 1.0.0
NuGet\Install-Package RePolicies.AspNetCore -Version 1.0.0
<PackageReference Include="RePolicies.AspNetCore" Version="1.0.0" />
<PackageVersion Include="RePolicies.AspNetCore" Version="1.0.0" />
<PackageReference Include="RePolicies.AspNetCore" />
paket add RePolicies.AspNetCore --version 1.0.0
#r "nuget: RePolicies.AspNetCore, 1.0.0"
#:package RePolicies.AspNetCore@1.0.0
#addin nuget:?package=RePolicies.AspNetCore&version=1.0.0
#tool nuget:?package=RePolicies.AspNetCore&version=1.0.0
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
RePoliciespackage — 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 HttpContext → Policy<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 | 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
- RePolicies (>= 1.0.0)
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 |