Configure authentication¶
HttpJsonClient supports three outbound authentication mechanisms — bearer token, HTTP Basic, and NTLM (explicit credentials or the current process identity) — configured independently and attached automatically to subsequent requests.
| Field | Value |
|---|---|
| Operation ID | OP-02 |
| Trigger | A consumer sets the BearerToken property, calls SetBasicAuthentication, or calls SetNtlmAuthentication on an initialized or uninitialized HttpJsonClient |
| Inputs | Bearer token string; Basic username/password; NTLM username/password, or none for current identity |
| Outputs | An HttpJsonClient instance configured to attach the selected credential(s) to outgoing requests |
| Primary components | HttpJsonClient.BearerToken, HttpJsonClient.SetBasicAuthentication, HttpJsonClient.SetNtlmAuthentication, HttpJsonClient.EncodeForBasicAuthentication |
| Entry point | src/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpJsonClient.cs |
Steps¶
- Bearer token: the consumer sets the
BearerTokenproperty to a non-empty string. The value is stored on the instance and attached as anAuthorizationheader on every subsequent request, regardless of header-persistence settings, because it is a property rather than aHeadersentry (BR-HTTP-08). - HTTP Basic: the consumer calls
SetBasicAuthentication(userName, password). - The method Base64-encodes
userName:passwordand addsAuthorization: Basic <encoded>to theHeadersdictionary (BR-HTTP-09). - On failure: if an
Authorizationkey already exists inHeaders— for example because headers were persisted from a previous request —Headers.AddthrowsArgumentException(BR-HTTP-16). - If
BearerTokenis also set, both credentials are attached and twoAuthorizationheader entries are sent on the same request; how the receiving stack resolves the duplicate is unverified**. - NTLM, explicit credentials: the consumer calls
SetNtlmAuthentication(userName, password). The resultingNtlmAuthenticatoris stored on the instance and assigned to everyRestClientcreated for a request (BR-HTTP-10). - NTLM, current identity: the consumer calls the parameterless
SetNtlmAuthentication(). RestSharp'sNtlmAuthenticator()maps to the process's default credentials*, and the resulting authenticator is likewise assigned to everyRestClientcreated for a request (BR-HTTP-10). - Credential persistence differs by mechanism: the
BearerTokenproperty and the NTLM authenticator are stored directly on the instance and are never cleared by the library, so they are attached to every subsequent request, including retries, for the instance's lifetime. The BasicAuthorizationheader instead lives in theHeadersdictionary, which is cleared after each request unlessPersistHeadersAfterRequestis set totrue(BR-HTTP-07) — so, with default options, Basic authentication must be re-applied with anotherSetBasicAuthenticationcall before each subsequent request, while bearer and NTLM credentials do not. See Execute request for how each mechanism is attached during request building.
Process flow¶
The following diagram visualizes the three independent authentication configuration paths and where each credential is attached.
flowchart TD
Start([Consumer configures authentication]) --> Choice{Mechanism?}
Choice -->|Bearer| Bearer["Set BearerToken property"]
Choice -->|Basic| Basic["SetBasicAuthentication(user, pw)"]
Choice -->|NTLM explicit| NtlmExp["SetNtlmAuthentication(user, pw)"]
Choice -->|NTLM current identity| NtlmCur["SetNtlmAuthentication()"]
Basic --> Exists{"Authorization key<br>already in Headers?"}
Exists -->|Yes| Throw(["ArgumentException"])
Exists -->|No| AddHeader["Add Authorization: Basic header"]
Bearer --> AttachedBearer(["Attached on every request<br>(never cleared)"])
AddHeader --> AttachedBasic(["Attached only until Headers<br>are cleared after the next request"])
NtlmExp --> AttachedAuth(["Authenticator assigned<br>to every new RestClient"])
NtlmCur --> AttachedAuth
Failure and edge-case behavior¶
SetBasicAuthenticationthrowsArgumentExceptionwhen called twice on a persisted instance without first removing the existingAuthorizationentry (BR-HTTP-16).- Combining
BearerTokenwith a Basic or customAuthorizationheader produces duplicateAuthorizationheader entries on the wire; receiving-side behavior is unverified**. - No scheme check exists on the base URL, so Basic and bearer credentials can be sent over a plain
http://endpoint.
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.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpJsonClient.cs