Home/Guides/.NET middleware: pseudonymize what your REST APIs receive, return and send

.NET middleware: pseudonymize what your REST APIs receive, return and send

The Sealforge.Anonymizer.AspNetCore package pseudonymizes the JSON and text bodies of your ASP.NET Core services: what they receive, what they return and what they send to other services (an LLM, a partner API). Detection runs on your Anonymizer server (Enterprise Edition) or on the online service; your business code does not change.

8 min read8 steps
0:00 / 0:00

What you need

The middleware comes with the Enterprise Edition (it calls your Anonymizer server, with its central policy and its audit) and with the online service (it calls the API hosted by Sealforge, with your licence key).

Your APIan ASP.NET Core project (Microsoft.NET.Sdk.Web), minimal APIs or controllers
The packageSealforge.Anonymizer.AspNetCore, supplied with your licence: a private NuGet feed or a folder holding the .nupkg
The sign-ininternal server: an API key sfk_… created in the server's administration; online service: your licence key SF-…
Warning. The package is not published on nuget.org: it comes from your customer area or a private feed.

Set it up from the desktop app

In the app, open Settings → .NET and Java middleware, choose the folder of your repository or project, then Scan: the app finds your ASP.NET Core projects and their endpoints (MapGet, MapPost…, [HttpGet]… actions) by reading the code, without building or running anything.

You give the server, the strategy, and for each endpoint whether its request or its response is pseudonymized. Preview the changes shows every file and the lines added; Apply writes them, after a copy of each file.

.csprojthe package reference
nuget.configthe feed you gave
appsettings.jsonthe Sealforge:Anonymizer section
Program.csAddSealforgeAnonymizer and UseSealforgeAnonymizer
endpoints.AnonymizeRequestBody() / .AnonymizeResponseBody(), or the attributes on an action
user secretsthe API key or the licence key, never in the repository
Tip. Every change is an insertion: nothing is removed or reformatted. Running the setup again with the same settings writes nothing more.

Or by hand: three lines

Add the package, then declare the middleware in Program.cs:

// Program.cs
builder.Services.AddSealforgeAnonymizer(builder.Configuration);   // section Sealforge:Anonymizer

var app = builder.Build();
app.UseSealforgeAnonymizer();
// appsettings.json
"Sealforge": { "Anonymizer": {
  "ServerUrl": "https://anonymizer.corp.example",
  "FailureMode": "Block"
} }

The API key is read like the rest of the configuration (Sealforge:Anonymizer:ApiKey): keep it in user secrets, an environment variable or a secret store.

Tip. The settings are checked at startup: one that does not fit stops the application with the reason, rather than failing at the first call.

Choose the endpoints to protect

app.MapPost("/tickets", (Ticket t) => repository.Save(t))
    .AnonymizeRequestBody();      // the endpoint, its logs and its database only see the pseudonymized body

app.MapGet("/customers/export", () => repository.All())
    .AnonymizeResponseBody();     // the caller receives the data pseudonymized

On a controller, the [AnonymizeRequestBody] and [AnonymizeResponseBody] attributes do the same.

Which body? The request, when the service itself must never hold the values (what it stores, logs or forwards). The response, when the service may hold them but not its caller (an export, a partner API, a front end). Both: a chat.

GET /customers/export

[ { "id": 1, "firstName": "[[FIRST_NAME_1]]", "lastName": "[[LAST_NAME_1]]", "email": "[[EMAIL_1]]" } ]

In a JSON body, only string values are pseudonymized; the structure, numbers, booleans and property names are kept. FieldMode and Fields limit the processing to some properties, or leave some out.

Warning. Keep personal data in strings: a phone number or an account stored as a JSON number is not pseudonymized. Files, forms and binary bodies pass as they are.

Outgoing calls: an LLM or a partner API

builder.Services.AddHttpClient("llm", http => http.BaseAddress = new Uri("https://api.openai.com"))
    .AddSealforgeAnonymization(outgoing => outgoing.Provider = "openai");

The body sent is pseudonymized; in the answer, the tokens become the real values again. The LLM only sees tokens, your code gets a readable answer.

To pseudonymize from your own code (a message queue, a batch), inject ISealforgeAnonymizer: AnonymizeTextAsync, AnonymizeJsonAsync and their restores.

Tip. Streamed answers (text/event-stream) are not restored: they keep their tokens. No value is lost, none leaks.

Strategies and categories

Pseudonymization is either explicit (you list the categories and their strategy) or named (a strategy saved on the server, called by its name); never both.

"Sealforge": { "Anonymizer": {
  "Strategy": "placeholder",
  "Categories": { "FirstName": "", "LastName": "", "Email": "hash", "PhoneNumber": "redact" }
} }
"Sealforge": { "Anonymizer": { "CentralStrategy": "rh-standard" } }

With Categories, only the listed categories are pseudonymized; an empty value applies Strategy. A named strategy is a global strategy saved in the desktop app and published to the server's central policy.

Warning. A misspelt category name stops the application at startup: it would otherwise be left in clear without your knowing. The server's policy always has the last word.

Internal server or online service

By default the middleware calls your Enterprise Edition server. For the online service, set Target = Online, the address of the service and your licence key:

builder.Services.AddSealforgeAnonymizer(o =>
{
    o.Target = AnonymizerTarget.Online;
    o.ServerUrl = "https://anonymizer.sealforge.fr";
    o.LicenseKey = builder.Configuration["Sealforge:LicenseKey"];
    o.PseudonymKey = builder.Configuration["Sealforge:PseudonymKey"];   // 16+
    o.CentralStrategy = "reversible";
});

With the reversible strategy, values are encrypted inside the tokens with your key (PseudonymKey, 16 characters or more): the answer of an outgoing call is restored from the tokens themselves, and nothing is kept in between.

Tip. LicenseKeyProvider and PseudonymKeyProvider give the key of each call: one licence and one key per tenant of your application, read from a secret store.
Warning. With the online service, content goes through our servers for the time of the request, without being kept. Units count as for the API: one per successful anonymization up to 100,000 characters.

If the server does not answer

FailureMode = Block (the default): server unreachable, licence refused or a body larger than MaxBodyBytes, the request is refused (503 or 413) and an outgoing call throws AnonymizationUnavailableException. Nothing leaves in clear.

PassThrough sends the body as it is and logs an error: keep it for data allowed to leave.

Tip. Every call is audited on the server, with the service's name and machine.