| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds HTTPS redirection middleware to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet allows you to add HTTPS redirection middleware to a Kestrun server instance. |
| | | 6 | | It can be used to enforce HTTPS by redirecting HTTP requests to HTTPS. |
| | | 7 | | .PARAMETER Options |
| | | 8 | | An instance of HttpsRedirectionOptions to configure the HTTPS redirection behavior. |
| | | 9 | | If this parameter is provided, it takes precedence over the individual parameters. |
| | | 10 | | .PARAMETER RedirectStatusCode |
| | | 11 | | The HTTP status code to use for redirection. Default is 307 (Temporary Redirect). |
| | | 12 | | This parameter is ignored if the Options parameter is provided. |
| | | 13 | | .PARAMETER HttpsPort |
| | | 14 | | The HTTPS port to which requests should be redirected. If not specified, the default port (443) is used. |
| | | 15 | | This parameter is ignored if the Options parameter is provided. |
| | | 16 | | .EXAMPLE |
| | | 17 | | Add-KrHttpsRedirection -Server $myServer -RedirectStatusCode 301 -HttpsPort 8443 |
| | | 18 | | Adds HTTPS redirection middleware to the specified Kestrun server instance, |
| | | 19 | | using a 301 (Permanent Redirect) status code and redirecting to port 8443. |
| | | 20 | | .EXAMPLE |
| | | 21 | | $options = [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]::new() |
| | | 22 | | $options.RedirectStatusCode = 308 |
| | | 23 | | $options.HttpsPort = 8443 |
| | | 24 | | Add-KrHttpsRedirection -Server $myServer -Options $options -PassThru |
| | | 25 | | Adds HTTPS redirection middleware to the specified Kestrun server instance, |
| | | 26 | | using the provided HttpsRedirectionOptions and returns the modified server instance. |
| | | 27 | | .NOTES |
| | | 28 | | This cmdlet is part of the Kestrun PowerShell module. |
| | | 29 | | #> |
| | | 30 | | function Add-KrHttpsRedirection { |
| | | 31 | | [KestrunRuntimeApi('Definition')] |
| | | 32 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 33 | | param( |
| | | 34 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 35 | | [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]$Options, |
| | | 36 | | |
| | | 37 | | [Parameter(ParameterSetName = 'Items')] |
| | | 38 | | [int]$RedirectStatusCode = [Microsoft.AspNetCore.Http.StatusCodes]::Status307TemporaryRedirect, |
| | | 39 | | |
| | | 40 | | [Parameter(ParameterSetName = 'Items')] |
| | | 41 | | [int]$HttpsPort |
| | | 42 | | ) |
| | | 43 | | # Ensure the server instance is resolved |
| | 0 | 44 | | $Server = Resolve-KestrunServer |
| | 0 | 45 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 46 | | # Create options from individual parameters |
| | 0 | 47 | | $Options = [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]::new() |
| | | 48 | | # Set default values |
| | 0 | 49 | | $Options.RedirectStatusCode = $RedirectStatusCode |
| | | 50 | | |
| | 0 | 51 | | if ($PSBoundParameters.ContainsKey('HttpsPort')) { $Options.HttpsPort = $HttpsPort } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | # Add the HTTPS redirection middleware |
| | 0 | 55 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddHttpsRedirection($Server, $Options) | Out-Null |
| | | 56 | | } |
| | | 57 | | |