< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrCorsPolicyMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCorsPolicyMiddleware.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 123
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/13/2025 - 17:19:56 Line coverage: 0% (0/20) Total lines: 115 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b10/13/2025 - 16:52:37 Line coverage: 0% (0/18) Total lines: 111 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/17/2025 - 15:48:30 Line coverage: 0% (0/23) Total lines: 123 Tag: Kestrun/Kestrun@b8199aff869a847b75e185d0527ba45e04a43d86

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCorsPolicyMiddleware.ps1

#LineLine coverage
 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 AllowAll
 26        If specified, allows any origin, method, and header to access the resources.
 27        This is a shorthand for specifying AllowAnyOrigin, AllowAnyMethod, and AllowAnyHeader
 28    .PARAMETER PassThru
 29        If specified, returns the modified server instance after adding the CORS policy.
 30    .EXAMPLE
 31        $server | Add-KrCorsPolicyMiddleware -Name 'AllowAll' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader
 32        This example adds a CORS policy named 'AllowAll' to the server, allowing any origin, method, and header.
 33    .EXAMPLE
 34        $server | Add-KrCorsPolicyMiddleware -Name 'CustomPolicy' -Builder $builder
 35        This example adds a CORS policy named 'CustomPolicy' to the server using the specified CORS policy builder.
 36    .EXAMPLE
 37        $server | Add-KrCorsPolicyMiddleware -Server $server -Name 'CustomPolicy' -AllowAnyOrigin -AllowAnyMethod -Allow
 38        This example adds a CORS policy named 'CustomPolicy' to the server, allowing any origin, method, and header.
 39    .NOTES
 40        This cmdlet is used to configure CORS policies for the Kestrun server, allowing you to control cross-origin requ
 41    .LINK
 42        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.cors.infrastructure.corspolicybuilder?view=asp
 43#>
 44function Add-KrCorsPolicyMiddleware {
 45    [KestrunRuntimeApi('Definition')]
 46    [CmdletBinding(defaultParameterSetName = 'Items')]
 47    [OutputType([Kestrun.Hosting.KestrunHost])]
 48    param(
 49        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 50        [Kestrun.Hosting.KestrunHost]$Server,
 51
 52        [Parameter(Mandatory = $true)]
 53        [string]$Name,
 54
 55        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 56        [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder,
 57
 58        [Parameter(ParameterSetName = 'Items')]
 59        [switch]$AllowAnyOrigin,
 60
 61        [Parameter(ParameterSetName = 'Items')]
 62        [switch]$AllowAnyMethod,
 63
 64        [Parameter(ParameterSetName = 'Items')]
 65        [switch]$AllowAnyHeader,
 66
 67        [Parameter(ParameterSetName = 'Items')]
 68        [switch]$AllowCredentials,
 69
 70        [Parameter(ParameterSetName = 'Items')]
 71        [switch]$DisallowCredentials,
 72
 73        [Parameter(ParameterSetName = 'AllowAll')]
 74        [switch]$AllowAll,
 75
 76        [Parameter()]
 77        [switch]$PassThru
 78    )
 79    begin {
 80        # Ensure the server instance is resolved
 081        $Server = Resolve-KestrunServer -Server $Server
 82    }
 83    process {
 084        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 85
 086            if ($AllowCredentials.IsPresent -and $DisallowCredentials.IsPresent) {
 087                throw 'Cannot specify both AllowCredentials and DisallowCredentials.'
 88            }
 89            # Build the CORS policy based on the specified parameters
 090            $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new()
 091            if ($AllowAnyOrigin.IsPresent) {
 092                $Builder.AllowAnyOrigin() | Out-Null
 93            }
 094            if ($AllowAnyMethod.IsPresent) {
 095                $Builder.AllowAnyMethod() | Out-Null
 96            }
 097            if ($AllowAnyHeader.IsPresent) {
 098                $Builder.AllowAnyHeader() | Out-Null
 99            }
 0100            if ($AllowCredentials.IsPresent) {
 0101                $Builder.AllowCredentials() | Out-Null
 102            }
 0103            if ($DisallowCredentials.IsPresent) {
 0104                $Builder.DisallowCredentials() | Out-Null
 105            }
 0106        } elseif ( $AllowAll.IsPresent ) {
 107            # If AllowAll is specified, configure the builder to allow any origin, method, and header
 0108            $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new()
 0109            $Builder.AllowAnyOrigin() | Out-Null
 0110            $Builder.AllowAnyMethod() | Out-Null
 0111            $Builder.AllowAnyHeader() | Out-Null
 112        }
 113
 0114        [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCors($Server, $Name, $Builder) | Out-Null
 115        # Add the CORS policy to the server
 116
 0117        if ($PassThru.IsPresent) {
 118            # if the PassThru switch is specified, return the modified server instance
 0119            return $Server
 120        }
 121    }
 122}
 123

Methods/Properties

Add-KrCorsPolicyMiddleware()