How many times have you reviewed a pull request in ASP.NET Core and spotted something like this?

Recent findings suggest that across enterprise .NET codebases, input canonicalization is often treated as an afterthought. It gets scattered across controllers, MediatR handlers, FluentValidation transform rules, and repository methods. (Related: How to generate text: using different decoding methods ...)

Inevitably, someone forgets a .Trim() on an endpoint:.

Recent findings suggest that to solve this, developers often reach for a global JsonConverter. But as many discover the hard way, global string converters introduce serious production bugs.

Here is why naive solutions break, and how we architected AutoNormalize—an open-source input canonicalization engine for ASP.NET Core, System.Text.Json, and EF Core that recently hit v1.0.0 on NuGet. (You can also try the live normalization rules and zero-allocation benchmarks in our interactive web sandbox). (Related: Porting fairseq wmt19 translation system to transformer...)

If you register a global string converter or reflectively trim every string property in a request, you immediately introduce a security vulnerability:.

The Fix: Defense-in-depth heuristic shielding. AutoNormalize automatically shields sensitive property names (Password, ApiKey, Token, Secret, Salt, Signature) by default, while providing explicit declarative attributes ([DoNotNormalize], [PreserveWhitespace]) for fine-grained control.

Modern C# promotes immutable data transfer objects:.

It has been revealed that if you attempt post-deserialization normalization via reflection or object traversal, you will hit a wall. Positional records have init-only properties and are instantiated via a primary constructor. Mutating them post-construction requires unsafe reflection or creating a fresh copy via with expressions, allocating unnecessary memory.

The Fix: In-Flight System.Text.Json Modifiers. Rather than touching the object after it is constructed, AutoNormalize hooks IJsonTypeInfoResolver.Modifiers in System.Text.Json. We attach a property-scoped converter during serialization metadata contract building:.

The string token is canonicalized directly off the Utf8JsonReader stream and passed into the constructor parameters at birth. It is 100% Native AOT compatible and preserves record immutability invariants.

In a recent development, if an API accepts a Dictionary, normalizing dictionary keys in-place can corrupt the underlying hash buckets. If keys "key " and "key" are normalized to the same string, your dictionary will throw an ArgumentException: An item with the same key has already been added.

Sources indicate that the Fix: Strict boundary invariants. AutoNormalize strictly normalizes dictionary values, never dictionary keys.

If every incoming API request and database entity passes through a normalization engine, overhead must be virtually unmeasurable.

For clean strings (strings that do not contain leading/trailing whitespace and are already in Unicode Normalization Form C), AutoNormalize implements a zero-allocation fast path:.

BenchmarkDotNet results on .NET 9.0 (Windows 11, RyuJIT AVX2):.

In a recent development, (Fun benchmarking lesson: During early microbenchmarks, our baseline measured 0.0077 ns—about 0.03 CPU cycles! RyuJIT had detected our compile-time string literal and constant-folded the call away. Always use runtime dynamic allocations like string.Concat in BenchmarkDotNet suites to prevent dead-code elimination.).

AutoNormalize is designed to be configured once at application startup.

One line wires up JSON options, Minimal API endpoint filters, and MVC model binders for query/route/form inputs:.

If you use Entity Framework Core, install AutoNormalize.EntityFrameworkCore to automatically intercept SaveChanges() and canonicalize entities before database persistence:.

Input canonicalization should be automatic, safe-by-default, and invisible to your everyday application logic.

AutoNormalize is fully open-source under the MIT license and multi-targets .NET 8.0, .NET 9.0, and .NET Standard 2.0.

We’d love to hear how your team at the moment handles input normalization. Have you encountered the password trimming or immutable record pitfalls in your own projects? Let's discuss in the comments below!

→ Read original article