Skip to content

Get the current user name

Resolves the current user's name from the client app's identity source (HttpContext.User for MVC, AuthenticationStateProvider for Blazor), optionally stripping the domain.

Field Value
Operation ID OP-08
Trigger Called by Configure an outbound client to send OnBehalfOfUser, or directly by consumer code
Inputs friendlyName (default true)
Outputs User name string, or null
Primary components ICurrentUserService.GetCurrentUser(friendlyName) — MvcCurrentUserService, BlazorCurrentUserService
Entry point src/SAIF.NetCore.NtlmApiAuth/src/SAIF.NetCore.NtlmApiAuth/Services/MvcCurrentUserService.cs, Services/BlazorCurrentUserService.cs

Steps

  1. MVC: read IHttpContextAccessor.HttpContext.User.Identity.Name (null-safe). Blazor: synchronously block on AuthenticationStateProvider.GetAuthenticationStateAsync() and read User.Identity.Name. (BR-NTLM-013)
  2. If friendlyName (the default), return the DomainlessUser() form of the name; otherwise return the raw name. Return null if there is no identity. (BR-NTLM-011)

Process flow

flowchart TD
    Start([GetCurrentUser friendlyName]) --> D{Client type}
    D -->|MVC| S1[Read HttpContext.User.Identity.Name]
    D -->|Blazor| S2["Block on GetAuthenticationStateAsync,<br>read User.Identity.Name"]
    S1 --> S3{friendlyName?}
    S2 --> S3
    S3 -->|Yes| S4[Return DomainlessUser name]
    S3 -->|No| S5[Return raw name]
    S4 --> End([Name or null])
    S5 --> End

Failure and edge-case behavior

  • BlazorCurrentUserService.GetCurrentUser blocks synchronously on an async call (GetAwaiter().GetResult()); this pattern can deadlock in some synchronization contexts, though no deadlock has been observed or tested in this run.*
  • Returns null when there is no HttpContext (MVC) or no authentication state (Blazor), e.g. in background work or Blazor prerendering.

Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.

Sources

  • src/SAIF.NetCore.NtlmApiAuth/src/SAIF.NetCore.NtlmApiAuth/Services/MvcCurrentUserService.cs
  • src/SAIF.NetCore.NtlmApiAuth/src/SAIF.NetCore.NtlmApiAuth/Services/BlazorCurrentUserService.cs