Register authentication and authorization services¶
The composition surface of the library: eight IServiceCollection/AuthenticationBuilder extension methods a consumer calls to wire identity, authorization, and authentication scheme registrations.
For the consumer-facing service/lifetime catalog, required external registrations, and the JWT-bearer-handler caveat, see Service registration.
| Field | Value |
|---|---|
| Operation ID | OP-14 |
| Trigger | A consuming application's Startup/composition code calls one or more AuthenticationServiceExtensions extension methods. |
| Inputs | The IServiceCollection being configured. |
| Outputs | DI registrations for identity services, the SecurityApi client, the outbound token client, and/or authentication/authorization building blocks; for the Add*Authentication methods, an AuthenticationBuilder. |
| Primary components | AuthenticationServiceExtensions.* (eight extension methods) |
| Entry point | src/SAIF.NetCore.AuthService/SAIF.NetCore.AuthService/SAIF.NetCore.AuthService/Extensions/AuthenticationServiceExtensions.cs |
Steps¶
AddAccessTokenClient()registersIAccessTokenClient→OktaTokenClient(Singleton).AddTokenIdentityService()registersISecurityClient→SecurityClient(Singleton),IIdentityService→TokenIdentityService(Scoped),IIdentityServiceAccessor→IdentityServiceAccessor(Transient).AddWinIdentityService()registers the same three services, substitutingWinIdentityService(Scoped) for theIIdentityService.AddIdentityAccessor()registersISecurityClient→SecurityClient(Singleton), bothWinIdentityServiceandTokenIdentityServiceas self (Scoped), andIIdentityServiceAccessor→MultiIdentityServiceAccessor(Transient).AddJwtAuthentication()/AddIISAuthentication()/AddDualAuthentication()each callAddAuthenticationwith the corresponding default scheme(s) and return the resultingAuthenticationBuilder;AddDualAuthenticationadditionally installs the"dual"policy scheme withForwardDefaultSelector = AuthenticationSchemeSelector.AddSecurityApiAuthorizeAttribute()registersIAuthorizationPolicyProvider→SecurityApiPolicy(Transient) andIAuthorizationHandler→SecurityApiHandler(Singleton).- All registrations use
Add*, notTryAdd*, so chaining several identity-registration methods registersISecurityClientmore than once (last one wins; harmless but redundant)*.
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Consumer calls extension methods]) --> Token[AddAccessTokenClient]
Start --> TokenId[AddTokenIdentityService]
Start --> WinId[AddWinIdentityService]
Start --> MultiId[AddIdentityAccessor]
Start --> Jwt[AddJwtAuthentication]
Start --> Iis[AddIISAuthentication]
Start --> Dual[AddDualAuthentication]
Start --> Authz[AddSecurityApiAuthorizeAttribute]
Dual --> Policy["dual scheme: ForwardDefaultSelector =<br>AuthenticationSchemeSelector"]
Authz --> Pipeline["SecurityApiPolicy + SecurityApiHandler registered"]
Failure and edge-case behavior¶
SecurityApiHandleris registered Singleton while a consumer may registerISecurityClientas Transient elsewhere (e.g.AppStartup), creating a lifetime mismatch where the Singleton handler captures oneSecurityClientinstance for the process lifetime*.- A consumer that registers
MultiIdentityServiceAccessormanually without also callingAddIdentityAccessorwill findGetServicereturnsnullforTokenIdentityService/WinIdentityService, since onlyAddIdentityAccessorregisters them as self*.
Related documentation¶
Note: Statements marked with
*were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with**could not be confirmed; treat as unknown until verified.
Sources¶
src/SAIF.NetCore.AuthService/SAIF.NetCore.AuthService/SAIF.NetCore.AuthService/Extensions/AuthenticationServiceExtensions.cs