Implement AI Data Redaction and PII Detection in ASP.NET Core

Redact personally identifiable information before user text reaches an AI provider. Use a dedicated ASP.NET Core service to apply policy consistently across workflows.

Architecture

Input → deterministic detectors and PII service → validated spans → redacted output

Workflow and system boundaries

Prerequisites and configuration

Set payload and language limits and decide which PII categories the application protects. Combine format-specific rules for local identifiers with the PII service for contextual entities. Send data only to approved services under the organization’s residency and retention policies. Deny raw-body logging at ingress and the client.

Implementation

Normalize entity offsets using the service’s selected index convention. Work with detected character spans instead of global replacements. Merge overlaps, then replace from the end toward the start so earlier positions stay valid. Return category counts and redacted text without copying original spans into the response. Protect or avoid a re-identification map.

Implementation example

foreach (var span in merged.OrderByDescending(s => s.Start))
    text = text.Remove(span.Start, span.Length).Insert(span.Start, "[REDACTED]");

Testing and troubleshooting

Try overlapping spans, repeated names, emoji before an identifier and a synthetic identifier embedded in unexpected punctuation. Simulate a detector outage and verify the application follows its fail-closed policy. Report precision and recall on representative languages and identifier formats.

Frequently asked question

Does PII detection anonymize text completely?

Context can still identify a person, and detectors can miss entities. Measure residual risk before sending data beyond the approved boundary.

Official documentation

Comments