< Summary - Kestrun — Combined Coverage

Information
Class: Public.Authentication.Add-KrCookiesAuthentication
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Authentication/Add-KrCookiesAuthentication.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 128
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 08/26/2025 - 14:53:17 Line coverage: 92.8% (13/14) Total lines: 96 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 22:37:32 Line coverage: 92.8% (13/14) Total lines: 97 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/11/2025 - 03:23:38 Line coverage: 92.8% (13/14) Total lines: 103 Tag: Kestrun/Kestrun@b7a12f5205442de0d384c1a861c1f8f30c74dcb510/13/2025 - 16:52:37 Line coverage: 0% (0/14) Total lines: 102 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e12/12/2025 - 17:27:19 Line coverage: 0% (0/18) Total lines: 128 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Authentication/Add-KrCookiesAuthentication.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Adds cookie authentication to the Kestrun server.
 4.DESCRIPTION
 5    Configures the Kestrun server to use cookie authentication for incoming requests.
 6.PARAMETER Server
 7    The Kestrun server instance to configure. If not specified, the current server instance is used.
 8.PARAMETER AuthenticationScheme
 9    The name of the cookie authentication scheme.
 10.PARAMETER DisplayName
 11    The display name for the authentication scheme.
 12.PARAMETER Description
 13    A description of the cookie authentication scheme.
 14.PARAMETER DocId
 15    Documentation IDs for the authentication scheme.
 16.PARAMETER Options
 17    The cookie authentication options to configure. If not specified, default options are used.
 18.PARAMETER ClaimPolicy
 19    The claim policy configuration to apply to the authentication scheme.
 20.PARAMETER SlidingExpiration
 21    Indicates whether the cookie expiration should be sliding. Defaults to false.
 22.PARAMETER LoginPath
 23    The path to the login page. If not specified, defaults to "/Account/Login".
 24.PARAMETER LogoutPath
 25    The path to the logout page. If not specified, defaults to "/Account/Logout".
 26.PARAMETER AccessDeniedPath
 27    The path to the access denied page. If not specified, defaults to "/Account/AccessDenied".
 28.PARAMETER ReturnUrlParameter
 29    The name of the query parameter used to return the URL after login. Defaults to "ReturnUrl".
 30.PARAMETER ExpireTimeSpan
 31    The time span after which the cookie expires. Defaults to 14 days.
 32.PARAMETER Cookie
 33    The cookie configuration to use. If not specified, default cookie settings are applied.
 34    Can be created with New-KrCookieBuilder and passed via pipeline.
 35.PARAMETER PassThru
 36    If specified, the cmdlet returns the modified server instance after configuration.
 37.EXAMPLE
 38    Add-KrCookiesAuthentication -Server $myServer -Name 'MyCookieAuth' -Options $myCookieOptions -ClaimPolicy $myClaimPo
 39    Adds cookie authentication to the specified Kestrun server with the provided options and claim policy.
 40.EXAMPLE
 41    Add-KrCookiesAuthentication -AuthenticationScheme 'MyCookieAuth' -SlidingExpiration -LoginPath '/Login' -LogoutPath 
 42    Configures cookie authentication with sliding expiration and custom paths for login, logout, and access denied
 43.EXAMPLE
 44    $cookie = New-KrCookieBuilder -Name 'AuthCookie' -HttpOnly -SameSite Lax
 45    Add-KrCookiesAuthentication -AuthenticationScheme 'MyCookieAuth' -Cookie $cookie -SlidingExpiration -ExpireTimeSpan 
 46    Configures cookie authentication using a custom cookie with HttpOnly and SameSite=Lax, along with sliding expiration
 47.EXAMPLE
 48    New-KrCookieBuilder -Name 'AuthCookie' -HttpOnly -SameSite Lax |
 49        Add-KrCookiesAuthentication -AuthenticationScheme 'MyCookieAuth' -SlidingExpiration -ExpireTimeSpan (New-TimeSpa
 50    Configures cookie authentication using a custom cookie created via pipeline with HttpOnly and SameSite=Lax, along wi
 51.NOTES
 52    This cmdlet is part of the Kestrun PowerShell module and is used to configure cookie authentication for Kestrun serv
 53.LINK
 54    https://docs.kestrun.dev/docs/powershell/kestrun/authentication
 55#>
 56function Add-KrCookiesAuthentication {
 57    [KestrunRuntimeApi('Definition')]
 58    [CmdletBinding(defaultParameterSetName = 'Items')]
 59    [OutputType([Kestrun.Hosting.KestrunHost])]
 60    param(
 61        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 62        [Kestrun.Hosting.KestrunHost]$Server,
 63        [Parameter()]
 64        [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::CookiesAuthenticationSchemeName
 65
 66        [Parameter()]
 67        [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::CookiesDisplayName,
 68
 69        [Parameter()]
 70        [string[]]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultDocumentationIds,
 71
 72        [Parameter(ParameterSetName = 'Items')]
 73        [string] $Description,
 74
 75        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 76        [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]$Options,
 77
 78        [Parameter()]
 79        [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy,
 80        [Parameter(ParameterSetName = 'Items')]
 81        [switch] $SlidingExpiration,
 82        [Parameter(ParameterSetName = 'Items')]
 83        [string]$LoginPath,
 84        [Parameter(ParameterSetName = 'Items')]
 85        [string]$LogoutPath,
 86        [Parameter(ParameterSetName = 'Items')]
 87        [string]$AccessDeniedPath,
 88        [Parameter(ParameterSetName = 'Items')]
 89        [string]$ReturnUrlParameter,
 90        [Parameter(ParameterSetName = 'Items')]
 91        [timespan] $ExpireTimeSpan,
 92        [Parameter(ParameterSetName = 'Items', ValueFromPipeline = $true)]
 93        [Microsoft.AspNetCore.Http.CookieBuilder]$Cookie,
 94        [Parameter()]
 95        [switch]$PassThru
 96    )
 97    process {
 98        # Ensure the server instance is resolved
 099        $Server = Resolve-KestrunServer -Server $Server
 100        # Build Options only when not provided directly
 0101        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0102            $Options = [Kestrun.Authentication.CookieAuthOptions]::new()
 103            # Set host reference
 0104            $Options.Host = $Server
 0105            if ($PSBoundParameters.ContainsKey('SlidingExpiration')) { $Options.SlidingExpiration = $SlidingExpiration.I
 0106            if ($PSBoundParameters.ContainsKey('LoginPath')) { $Options.LoginPath = $LoginPath }
 0107            if ($PSBoundParameters.ContainsKey('LogoutPath')) { $Options.LogoutPath = $LogoutPath }
 0108            if ($PSBoundParameters.ContainsKey('AccessDeniedPath')) { $Options.AccessDeniedPath = $AccessDeniedPath }
 0109            if ($PSBoundParameters.ContainsKey('ReturnUrlParameter')) { $Options.ReturnUrlParameter = $ReturnUrlParamete
 0110            if ($PSBoundParameters.ContainsKey('ExpireTimeSpan')) { $Options.ExpireTimeSpan = $ExpireTimeSpan }
 0111            if ($PSBoundParameters.ContainsKey('Cookie')) { $Options.Cookie = $Cookie }
 112
 0113            if (-not ([string]::IsNullOrWhiteSpace($Description))) {
 0114                $Options.Description = $Description
 115            }
 116            # OpenAPI documentation IDs
 0117            $Options.DocumentationId = $DocId
 118        }
 119        # Add cookie authentication to the server
 0120        [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddCookieAuthentication(
 0121            $Server, $AuthenticationScheme, $DisplayName, $Options, $ClaimPolicy) | Out-Null
 122
 123        # Return the modified server instance if PassThru is specified
 0124        if ($PassThru.IsPresent) {
 0125            return $Server
 126        }
 127    }
 128}

Methods/Properties

Add-KrCookiesAuthentication()