Skip to content

HttpContextExtensions

HttpContextExtensions is a static extension-method class that normalizes an ambient ASP.NET user's identity name to a bare account name, stripping a down-level domain prefix or UPN suffix. JsonHttpClient uses it internally to build the OnBehalfOfUser header, and it is also directly callable by consumers.

Purpose

  • Normalize HttpContext.User.Identity.Name to a bare account name by removing a DOMAIN\ prefix or @domain suffix, so downstream APIs receive an unqualified account name (BR-CLI-004). See DomainlessUser.

Context

  • Namespace: SAIF.Web.API.Clients.Extensions
  • Assembly/package: SAIF.Web.API.Clients (NuGet package SAIF.Web.API.Clients)

Public members

Member Signature Purpose
DomainlessUser static string DomainlessUser(this HttpContext httpContext) Reads httpContext.User.Identity.Name, strips a DOMAIN\ prefix (when the separator index is > 1) and an @domain suffix (when its index in the original string is > 1), and returns the normalized name.

Behavioral notes

  • Throws NullReferenceException if httpContext, User, Identity, or Name is null — there is no guard. For an unauthenticated request, Name is "".
  • A separator at index 0 or 1 (for example a name beginning A\user) is not stripped, because the threshold is > 1, not >= 0.
  • The @ index is computed on the original string before the \-prefix substring is taken; a name containing both separators (for example DOMAIN\user@domain) can be truncated incorrectly as a result.

Constraints and usage notes

  • This extension method requires an ambient ASP.NET request context; it is not usable from a non-web host (console/service) without one.

Sources

  • src/SAIF.Web.API.Clients/Extensions/HttpContextExtensions.cs