| | | 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 Server |
| | | 8 | | The Kestrun server instance to which the HTTPS redirection middleware will be added. |
| | | 9 | | .PARAMETER Options |
| | | 10 | | An instance of HttpsRedirectionOptions to configure the HTTPS redirection behavior. |
| | | 11 | | If this parameter is provided, it takes precedence over the individual parameters. |
| | | 12 | | .PARAMETER RedirectStatusCode |
| | | 13 | | The HTTP status code to use for redirection. Default is 307 (Temporary Redirect). |
| | | 14 | | This parameter is ignored if the Options parameter is provided. |
| | | 15 | | .PARAMETER HttpsPort |
| | | 16 | | The HTTPS port to which requests should be redirected. If not specified, the default port (443) is used. |
| | | 17 | | This parameter is ignored if the Options parameter is provided. |
| | | 18 | | .PARAMETER PassThru |
| | | 19 | | If specified, the cmdlet returns the modified Kestrun server instance. |
| | | 20 | | .EXAMPLE |
| | | 21 | | Add-KrHttpsRedirection -Server $myServer -RedirectStatusCode 301 -HttpsPort 8443 |
| | | 22 | | Adds HTTPS redirection middleware to the specified Kestrun server instance, |
| | | 23 | | using a 301 (Permanent Redirect) status code and redirecting to port 8443. |
| | | 24 | | .EXAMPLE |
| | | 25 | | $options = [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]::new() |
| | | 26 | | $options.RedirectStatusCode = 308 |
| | | 27 | | $options.HttpsPort = 8443 |
| | | 28 | | Add-KrHttpsRedirection -Server $myServer -Options $options -PassThru |
| | | 29 | | Adds HTTPS redirection middleware to the specified Kestrun server instance, |
| | | 30 | | using the provided HttpsRedirectionOptions and returns the modified server instance. |
| | | 31 | | .NOTES |
| | | 32 | | This cmdlet is part of the Kestrun PowerShell module. |
| | | 33 | | #> |
| | | 34 | | function Add-KrHttpsRedirection { |
| | | 35 | | [KestrunRuntimeApi('Definition')] |
| | | 36 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 37 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 38 | | param( |
| | | 39 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 40 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 41 | | |
| | | 42 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 43 | | [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]$Options, |
| | | 44 | | |
| | | 45 | | [Parameter(ParameterSetName = 'Items')] |
| | | 46 | | [int]$RedirectStatusCode = [Microsoft.AspNetCore.Http.StatusCodes]::Status307TemporaryRedirect, |
| | | 47 | | |
| | | 48 | | [Parameter(ParameterSetName = 'Items')] |
| | | 49 | | [int]$HttpsPort, |
| | | 50 | | [Parameter()] |
| | | 51 | | [switch]$PassThru |
| | | 52 | | ) |
| | | 53 | | begin { |
| | | 54 | | # Ensure the server instance is resolved |
| | 0 | 55 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 56 | | } |
| | | 57 | | process { |
| | 0 | 58 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 59 | | # Create options from individual parameters |
| | 0 | 60 | | $Options = [Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionOptions]::new() |
| | | 61 | | # Set default values |
| | 0 | 62 | | $Options.RedirectStatusCode = $RedirectStatusCode |
| | | 63 | | |
| | 0 | 64 | | if ($PSBoundParameters.ContainsKey('HttpsPort')) { $Options.HttpsPort = $HttpsPort } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | # Add the HTTPS redirection middleware |
| | 0 | 68 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddHttpsRedirection($Server, $Options) | Out-Null |
| | | 69 | | |
| | 0 | 70 | | if ($PassThru.IsPresent) { |
| | | 71 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 72 | | return $Server |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |