Skip to content

Propagate authenticated user name from HTTP to Marten LastModifiedBy#2264

Merged
jeremydmiller merged 1 commit intomainfrom
feature/marten-session-username-from-http
Mar 5, 2026
Merged

Propagate authenticated user name from HTTP to Marten LastModifiedBy#2264
jeremydmiller merged 1 commit intomainfrom
feature/marten-session-username-from-http

Conversation

@jeremydmiller
Copy link
Member

Summary

  • Adds EnableRelayOfUserName option to WolverineOptions that, when enabled, automatically extracts HttpContext.User.Identity.Name and propagates it through the messaging infrastructure
  • Sets IMessageContext.UserName from the authenticated ClaimsPrincipal, carries it on outgoing Envelope.UserName, and applies it to Marten's IDocumentSession.LastModifiedBy
  • Adds OpenTelemetry enduser.id tag on the current activity

Changes

  • Core: Added UserName property to Envelope, IMessageContext, MessageBus, MessageContext, TestMessageContext, and EnvelopeConstants
  • Serialization: EnvelopeSerializer reads/writes UserName for binary round-trip
  • Relay: MessageBus.TrackEnvelopeCorrelation() propagates UserName to outgoing envelopes when enabled
  • HTTP Middleware: New UserNameMiddleware + UserNamePolicy (follows RequestIdMiddleware pattern) registered in WolverineHttpOptions
  • Marten: OutboxedSessionFactory.configureSession() sets session.LastModifiedBy from UserName
  • Docs: Added "Tracking User Name" section to HTTP security documentation

Test plan

  • Unit: EnvelopeSerializer round-trip for UserName (set and null)
  • Unit: TrackEnvelopeCorrelation relays UserName when enabled, skips when disabled, doesn't override existing
  • Unit: ReadEnvelope reads UserName from incoming envelope
  • Integration: HTTP endpoint returns UserName from authenticated ClaimsPrincipal
  • Integration: Returns "NONE" when not authenticated

Closes #2203

🤖 Generated with Claude Code

…essaging to Marten LastModifiedBy

When EnableRelayOfUserName is true, automatically extract the user name from
HttpContext.User.Identity.Name, set it on IMessageContext.UserName, propagate it
on outgoing envelopes, and apply it to Marten IDocumentSession.LastModifiedBy.
Also adds an OpenTelemetry enduser.id tag.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatic middleware for Marten IDocumentSession + HTTP for the user name

1 participant