In an ASP.NET Core claims gateway using HTTP logging, how do you log X-Correlation-Id while keeping Authorization and member ids out?
answer
- an allowlist, not a blocklist
- unlisted values become [Redacted]
- options.RequestHeaders.Add(...)
- path and query can leak ids too
- AddHttpLoggingRedaction with data classes
basics
~20 sHTTP logging prints header values only for names in HttpLoggingOptions.RequestHeaders; every other header appears with the value [Redacted]. Add X-Correlation-Id to that set, never Authorization, and keep member ids out of logged paths and queries.
solid answer
~40 s`HttpLoggingOptions.RequestHeaders` and `ResponseHeaders` are case-insensitive **allowlists**. The defaults hold safe protocol headers such as `Accept`, `Content-Type`, `Host` and `User-Agent`; any other header present on the request is still logged by name, but with the value `[Redacted]`. So `Authorization`, `Cookie`, `X-Api-Key` and `X-Member-Id` are already hidden, and you opt in with `o.RequestHeaders.Add("X-Correlation-Id")`. The leaks come from elsewhere: adding a credential header to the set, enabling `RequestQuery` when ids travel in the query, or a route such as `/members/{memberId}/claims` whose raw path is logged. For that, `AddHttpLoggingRedaction` from `Microsoft.AspNetCore.Diagnostics.Middleware` (still `[Experimental]` in dotnet/extensions 10.10.1) takes over: it logs only headers listed in `RequestHeadersDataClasses`, runs each through the redactor for its data classification (`AddRedaction` supplies them), and redacts route parameters.
code
http · 7 linesPOST /claims HTTP/1.1
Host: gateway.example
Authorization: Bearer eyJhbGciOi...
X-Api-Key: 7f3c...
X-Member-Id: M-48812
X-Correlation-Id: 0d5e2c1a-claims-77
Content-Type: application/jsongo deeper
Recall that only allowlisted headers show values, that others appear as [Redacted], and that RequestHeaders.Add opts one in.
Explain the allowlist for requests and responses, why the query string is off by default, and the path and body leaks header settings cannot stop.
Show when to move to AddHttpLoggingRedaction: data classifications per header and route parameter, Strict path redaction, excluded paths, and its experimental status.
Own the data-classification taxonomy shared by logs and telemetry, and decide which teams may widen an allowlist and how such a change is reviewed.
## How the middleware treats headers With `HttpLoggingFields.RequestHeaders` enabled (it is in the default), the middleware walks **every** header on the request and compares each name with `HttpLoggingOptions.RequestHeaders`, a case-insensitive `ISet<string>`: - name in the set: logged as `Name: value`; - name not in the set: logged as `Name: [Redacted]`. Response headers go through the same check against `ResponseHeaders`. The model is an **allowlist**: a new credential header a partner invents tomorrow is redacted without anyone touching configuration. The default request allowlist has 26 protocol headers, among them `Accept`, `Accept-Encoding`, `Cache-Control`, `Connection`, `Content-Length`, `Content-Type`, `Host`, `Range`, `Transfer-Encoding` and `User-Agent`. The response allowlist has 19, such as `Content-Type`, `Content-Length`, `Date`, `Location` and `Server`. `Authorization`, `Cookie` and `Set-Cookie` are in neither. ## The claims gateway Partners call `POST /claims` with `Authorization: Bearer ...`, an `X-Api-Key`, an `X-Member-Id` and an `X-Correlation-Id` the support team needs to trace a claim end to end. ```csharp builder.Services.AddHttpLogging(o => { o.LoggingFields = HttpLoggingFields.RequestPropertiesAndHeaders | HttpLoggingFields.ResponsePropertiesAndHeaders; o.RequestHeaders.Add("X-Correlation-Id"); o.ResponseHeaders.Add("X-Correlation-Id"); }); ``` Result: the correlation id is logged in full; `Authorization`, `X-Api-Key` and `X-Member-Id` appear only as `[Redacted]`. The header **names** remain visible, which is useful ("the partner did send a key") and harmless. ## Where secrets still leak 1. **Adding the wrong name.** `o.RequestHeaders.Add("Authorization")` to debug a 401 writes every bearer token into the log store. Debug token problems from the token's claims, not from logs. 2. **The query string.** `RequestQuery` is off by default; turning it on logs `?memberId=...` verbatim. 3. **The path.** `RequestPath` logs the raw path, so `/members/M-48812/claims` carries the member id regardless of header settings. 4. **Bodies.** A logged claim body contains diagnoses and member data; header settings do nothing for it. ## AddHttpLoggingRedaction The `Microsoft.AspNetCore.Diagnostics.Middleware` package from dotnet/extensions adds `AddHttpLoggingRedaction`, which plugs an `IHttpLoggingInterceptor` into the same middleware: ```csharp // MyTaxonomy is the team's own DataClassification definitions builder.Services.AddRedaction(); // supplies IRedactorProvider builder.Services.AddHttpLoggingRedaction(o => { o.RequestHeadersDataClasses.Add("X-Member-Id", MyTaxonomy.Personal); o.RouteParameterDataClasses.Add("memberId", MyTaxonomy.Personal); o.ExcludePathStartsWith.Add("/health"); }); app.UseHttpLogging(); ``` What changes compared with the plain middleware: | Aspect | Plain `AddHttpLogging` | With `AddHttpLoggingRedaction` | |---|---|---| | Unlisted header | name logged, value `[Redacted]` | not logged at all | | Listed header | value as-is | value passed through the redactor for its `DataClassification` | | Header key | `X-Correlation-Id` | `http.request.header.x-correlation-id` style | | Path | raw path | route template with parameters redacted (`RequestPathParameterRedactionMode` defaults to `Strict`) | | Excluded paths | none | `ExcludePathStartsWith` turns logging off for them | | Entries per request | several | one, because it sets `CombineLogs = true` and adds `Duration` | The redactors come from the compliance libraries: the interceptor's constructor needs an `IRedactorProvider`, which `AddRedaction()` registers, and the documentation's sample output shows the erasing redactor removing classified values. A header you want readable, such as `X-Correlation-Id`, must therefore be mapped too, with a classification whose configured redactor keeps the value; an unmapped header simply disappears. `AddHttpLoggingRedaction` and `LoggingRedactionOptions` are marked `[Experimental]` in 10.10.1, so the compiler reports an experimental-API diagnostic you must suppress, and option validation runs at start-up.
- A teammate adds "Authorization" to RequestHeaders for a day to debug 401s. What is the damage and the better approach?Every bearer token sent during that day is now in the log store and in every copy or export of it, readable by anyone with log access until it expires. Remove the entry, treat the tokens as exposed, and debug 401s from the authentication handler's own failure logs and the token's claims instead.
- How does HttpLoggingRedaction's Strict path mode treat a route parameter nobody classified?In `Strict` mode, the default for `RequestPathParameterRedactionMode`, route parameters are treated as sensitive: one without an entry in `RouteParameterDataClasses` is redacted. `Loose` treats unclassified parameters as safe and logs them as-is, and `None` redacts nothing at all.
- Headers not in RequestHeaders are dropped, so nobody can tell they were sent.
- Authorization is logged by default and must be removed with a blocklist.
- Allowlisting a correlation header also exposes the other custom headers.
- Header redaction also protects member ids carried in the path or query.
- AddHttpLoggingRedaction replaces UseHttpLogging, so the middleware is not needed.
Assumes ASP.NET Core 10 with Microsoft.AspNetCore.Diagnostics.Middleware 10.10.1, where AddHttpLoggingRedaction and LoggingRedactionOptions are still marked [Experimental].