| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds a CORS policy to the server. |
| | 4 | | .DESCRIPTION |
| | 5 | | This cmdlet allows you to configure a CORS policy for the Kestrun server. |
| | 6 | | It can be used to specify allowed origins, methods, headers, and other CORS settings. |
| | 7 | | .PARAMETER Server |
| | 8 | | The Kestrun server instance to which the CORS policy will be added. |
| | 9 | | .PARAMETER Name |
| | 10 | | The name of the CORS policy. |
| | 11 | | .PARAMETER Builder |
| | 12 | | The CORS policy builder to configure the CORS policy. |
| | 13 | | .PARAMETER AllowAnyOrigin |
| | 14 | | If specified, allows any origin to access the resources. |
| | 15 | | .PARAMETER AllowAnyMethod |
| | 16 | | If specified, allows any HTTP method to be used in requests. |
| | 17 | | .PARAMETER AllowAnyHeader |
| | 18 | | If specified, allows any header to be included in requests. |
| | 19 | | If not specified, only headers explicitly allowed will be included. |
| | 20 | | .PARAMETER AllowCredentials |
| | 21 | | If specified, allows credentials (cookies, authorization headers, etc.) to be included in requests. |
| | 22 | | .PARAMETER DisallowCredentials |
| | 23 | | If specified, disallows credentials in requests. |
| | 24 | | If not specified, credentials will be allowed. |
| | 25 | | .PARAMETER PassThru |
| | 26 | | If specified, returns the modified server instance after adding the CORS policy. |
| | 27 | | .EXAMPLE |
| | 28 | | $server | Add-KrCorsPolicy -Name 'AllowAll' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader |
| | 29 | | This example adds a CORS policy named 'AllowAll' to the server, allowing any origin, method, and header. |
| | 30 | | .EXAMPLE |
| | 31 | | $server | Add-KrCorsPolicy -Name 'CustomPolicy' -Builder $builder |
| | 32 | | This example adds a CORS policy named 'CustomPolicy' to the server using the specified CORS policy builder. |
| | 33 | | .EXAMPLE |
| | 34 | | $server | Add-KrCorsPolicy -Server $server -Name 'CustomPolicy' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader |
| | 35 | | This example adds a CORS policy named 'CustomPolicy' to the server, allowing any origin, method, and header. |
| | 36 | | .NOTES |
| | 37 | | This cmdlet is used to configure CORS policies for the Kestrun server, allowing you to control cross-origin requ |
| | 38 | | .LINK |
| | 39 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.cors.infrastructure.corspolicybuilder?view=asp |
| | 40 | | #> |
| | 41 | | function Add-KrCorsPolicy { |
| | 42 | | [KestrunRuntimeApi('Definition')] |
| | 43 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | 44 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 45 | | param( |
| | 46 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 47 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 48 | |
|
| | 49 | | [Parameter(Mandatory = $true)] |
| | 50 | | [string]$Name, |
| | 51 | |
|
| | 52 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | 53 | | [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder, |
| | 54 | |
|
| | 55 | | [Parameter(ParameterSetName = 'Items')] |
| | 56 | | [switch]$AllowAnyOrigin, |
| | 57 | |
|
| | 58 | | [Parameter(ParameterSetName = 'Items')] |
| | 59 | | [switch]$AllowAnyMethod, |
| | 60 | |
|
| | 61 | | [Parameter(ParameterSetName = 'Items')] |
| | 62 | | [switch]$AllowAnyHeader, |
| | 63 | |
|
| | 64 | | [Parameter(ParameterSetName = 'Items')] |
| | 65 | | [switch]$AllowCredentials, |
| | 66 | |
|
| | 67 | | [Parameter(ParameterSetName = 'Items')] |
| | 68 | | [switch]$DisallowCredentials, |
| | 69 | |
|
| | 70 | | [Parameter()] |
| | 71 | | [switch]$PassThru |
| | 72 | | ) |
| | 73 | | begin { |
| | 74 | | # Ensure the server instance is resolved |
| 0 | 75 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 76 | | if ($null -eq $Server) { |
| 0 | 77 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 78 | | } |
| | 79 | | } |
| | 80 | | process { |
| 0 | 81 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | 82 | |
|
| 0 | 83 | | if ($AllowCredentials.IsPresent -and $DisallowCredentials.IsPresent) { |
| 0 | 84 | | throw 'Cannot specify both AllowCredentials and DisallowCredentials.' |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new() |
| 0 | 88 | | if ($AllowAnyOrigin.IsPresent) { |
| 0 | 89 | | $Builder.AllowAnyOrigin() | Out-Null |
| | 90 | | } |
| 0 | 91 | | if ($AllowAnyMethod.IsPresent) { |
| 0 | 92 | | $Builder.AllowAnyMethod() | Out-Null |
| | 93 | | } |
| 0 | 94 | | if ($AllowAnyHeader.IsPresent) { |
| 0 | 95 | | $Builder.AllowAnyHeader() | Out-Null |
| | 96 | | } |
| 0 | 97 | | if ($AllowCredentials.IsPresent) { |
| 0 | 98 | | $Builder.AllowCredentials() | Out-Null |
| | 99 | | } |
| 0 | 100 | | if ($DisallowCredentials.IsPresent) { |
| 0 | 101 | | $Builder.DisallowCredentials() | Out-Null |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddCors($Server, $Name, $Builder) | Out-Null |
| | 106 | | # Add the CORS policy to the server |
| | 107 | |
|
| 0 | 108 | | if ($PassThru.IsPresent) { |
| | 109 | | # if the PassThru switch is specified, return the server instance |
| | 110 | | # Return the modified server instance |
| 0 | 111 | | return $Server |
| | 112 | | } |
| | 113 | | } |
| | 114 | | } |
| | 115 | |
|