Enforce HTTPS on an action¶
RequireSSLAttribute.OnActionExecuting(HttpActionContext) redirects a non-HTTPS request to HTTPS unless a local-request exemption applies.
| Field | Value |
|---|---|
| Operation ID | OP-07 |
| Trigger | Web API action-filter pipeline, before action execution, on any decorated controller/action. |
| Inputs | HttpActionContext, appSettings["RequireSSLForLocal"] |
| Outputs | none (side effect: HTTP redirect) |
| Primary components | RequireSSLAttribute.OnActionExecuting, RequireSSLAttribute.RequiresSSL |
| Entry point | src/SAIF.Web.API/RequireSSLAttribute.cs |
Steps¶
- Evaluate
actionContext.Request.RequestUri.Scheme.ToLower() != Uri.UriSchemeHttps. - If already HTTPS, skip to step 6.
- Call
RequiresSSL: initializerequireSSL = true(BR-WEBAPI-012). - Secure-by-default.
- If
actionContext.Request.IsLocal(), look for an app-setting key literally equal to"RequireSSLForLocal"inConfigurationManager.AppSettings.AllKeys(BR-WEBAPI-012). - Non-local requests always require SSL.
SAIF.Common.Extensions.IsLocal()is implemented inSAIF.Framework.Core; its internal steps could not be embedded — the package is not indexed for this run.**- If the key exists,
bool.TryParseits value intorequireSSL; if the key is absent, setrequireSSL = false(BR-WEBAPI-012). - An unparseable value leaves
requireSSL = false(a failedTryParsesets theoutparameter tofalse). - If the scheme is not HTTPS and
RequiresSSLreturned true, buildnew UriBuilder(requestUri) { Scheme = Uri.UriSchemeHttps, Port = 443 }and callHttpContext.Current.Response.Redirect(BR-WEBAPI-013). - Uses
HttpContext.Current(web-host only);Response.RedirectwithoutendResponse:falseraisesThreadAbortExceptionby design.* - Call
base.OnActionExecuting(actionContext).
Process flow¶
flowchart TD
Start([Action filter pipeline]) --> Scheme{Scheme is HTTPS?}
Scheme -->|Yes| Base[base.OnActionExecuting]
Scheme -->|No| Init["requireSSL = true<br>(default)"]
Init --> Local{IsLocal request?}
Local -->|No| Redirect
Local -->|Yes| KeyCheck{"RequireSSLForLocal key<br>present?"}
KeyCheck -->|No| NotRequired["requireSSL = false"]
KeyCheck -->|Yes| Parse["bool.TryParse value into<br>requireSSL"]
NotRequired --> Decide{requireSSL?}
Parse --> Decide
Decide -->|Yes| Redirect["Redirect to HTTPS on<br>port 443"]
Decide -->|No| Base
Redirect --> Base
Failure and edge-case behavior¶
HttpContext.Currentis web-host only; a self-hosted scenario would seenullat redirect time.*- The redirect raises
ThreadAbortExceptionby design (noendResponse:falseargument supplied).*
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.
Related documentation¶
Sources¶
src/SAIF.Web.API/RequireSSLAttribute.cs