@@ -33,6 +33,31 @@ public class StaticWebAppsClientPrincipal
3333 public IEnumerable < string > ? UserRoles { get ; set ; }
3434 }
3535
36+ /// <summary>
37+ /// Environment variable key set by the Azure platform for every App Service and Static Web Apps
38+ /// hosted site. Its presence is the best-effort signal that the runtime is executing behind an
39+ /// Azure-managed proxy that injects and validates the X-MS-CLIENT-PRINCIPAL header.
40+ /// </summary>
41+ public const string WEBSITE_SITE_NAME_ENVVAR = "WEBSITE_SITE_NAME" ;
42+
43+ // ── StaticWebApps messages ───────────────────────────────────────────────────────────────────
44+ /// <summary>
45+ /// Error message used when StaticWebApps Authentication is configured in production mode
46+ /// without a detectable Azure-hosted environment.
47+ /// </summary>
48+ public const string SWA_PROD_MISSING_ENV_CONFIG =
49+ "StaticWebApps environment not detected while runtime is in production mode. " +
50+ "The X-MS-CLIENT-PRINCIPAL header is not cryptographically validated by DAB and can be trivially " +
51+ "forged when the service is not hosted behind an Azure Static Web Apps proxy. " +
52+ "Set host.mode to 'development' for local testing, or deploy behind Azure Static Web Apps." ;
53+
54+ /// <summary>
55+ /// Warning message logged when StaticWebApps environment not detected (applicable to development mode).
56+ /// </summary>
57+ public const string SWA_DEV_MISSING_ENV_CONFIG =
58+ "StaticWebApps environment not detected. The X-MS-CLIENT-PRINCIPAL header is not cryptographically " +
59+ "validated; EasyAuth authentication may not behave as expected outside an Azure Static Web Apps environment." ;
60+
3661 /// <summary>
3762 /// Base64 decodes and deserializes the x-ms-client-principal payload containing
3863 /// SWA token metadata.
@@ -98,4 +123,14 @@ error is NotSupportedException ||
98123
99124 return identity ;
100125 }
126+
127+ /// <summary>
128+ /// Returns a best-effort indication that the runtime is executing inside an Azure Static Web Apps
129+ /// environment by checking for the presence of <see cref="WEBSITE_SITE_NAME_ENVVAR"/>, which is
130+ /// injected by the Azure platform for every SWA-hosted application.
131+ /// </summary>
132+ public static bool AreExpectedSWAEnvVarsPresent ( )
133+ {
134+ return ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( WEBSITE_SITE_NAME_ENVVAR ) ) ;
135+ }
101136}
0 commit comments