< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 102
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 - 01:25:22 Line coverage: 92.8% (13/14) Total lines: 96 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/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@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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 Name
 9    The name of the cookie authentication scheme.
 10.PARAMETER Options
 11    The cookie authentication options to configure. If not specified, default options are used.
 12.PARAMETER ClaimPolicy
 13    The claim policy configuration to apply to the authentication scheme.
 14.PARAMETER SlidingExpiration
 15    Indicates whether the cookie expiration should be sliding. Defaults to false.
 16.PARAMETER LoginPath
 17    The path to the login page. If not specified, defaults to "/Account/Login".
 18.PARAMETER LogoutPath
 19    The path to the logout page. If not specified, defaults to "/Account/Logout".
 20.PARAMETER AccessDeniedPath
 21    The path to the access denied page. If not specified, defaults to "/Account/AccessDenied".
 22.PARAMETER ReturnUrlParameter
 23    The name of the query parameter used to return the URL after login. Defaults to "ReturnUrl".
 24.PARAMETER ExpireTimeSpan
 25    The time span after which the cookie expires. Defaults to 14 days.
 26.PARAMETER Cookie
 27    The cookie configuration to use. If not specified, default cookie settings are applied.
 28    Can be created with New-KrCookieBuilder and passed via pipeline.
 29.PARAMETER PassThru
 30    If specified, the cmdlet returns the modified server instance after configuration.
 31.EXAMPLE
 32    Add-KrCookiesAuthentication -Server $myServer -Name 'MyCookieAuth' -Options $myCookieOptions -ClaimPolicy $myClaimPo
 33    Adds cookie authentication to the specified Kestrun server with the provided options and claim policy.
 34.EXAMPLE
 35    Add-KrCookiesAuthentication -Name 'MyCookieAuth' -SlidingExpiration -LoginPath '/Login' -LogoutPath '/Logout' -Acces
 36    Configures cookie authentication with sliding expiration and custom paths for login, logout, and access denied
 37.EXAMPLE
 38    $cookie = New-KrCookieBuilder -Name 'AuthCookie' -HttpOnly -SameSite Lax
 39    Add-KrCookiesAuthentication -Name 'MyCookieAuth' -Cookie $cookie -SlidingExpiration -ExpireTimeSpan (New-TimeSpan -D
 40    Configures cookie authentication using a custom cookie with HttpOnly and SameSite=Lax, along with sliding expiration
 41.EXAMPLE
 42    New-KrCookieBuilder -Name 'AuthCookie' -HttpOnly -SameSite Lax |
 43        Add-KrCookiesAuthentication -Name 'MyCookieAuth' -SlidingExpiration -ExpireTimeSpan (New-TimeSpan -Days 7)
 44    Configures cookie authentication using a custom cookie created via pipeline with HttpOnly and SameSite=Lax, along wi
 45.NOTES
 46    This cmdlet is part of the Kestrun PowerShell module and is used to configure cookie authentication for Kestrun serv
 47.LINK
 48    https://docs.kestrun.dev/docs/powershell/kestrun/authentication
 49#>
 50function Add-KrCookiesAuthentication {
 51    [KestrunRuntimeApi('Definition')]
 52    [CmdletBinding(defaultParameterSetName = 'Items')]
 53    [OutputType([Kestrun.Hosting.KestrunHost])]
 54    param(
 55        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 56        [Kestrun.Hosting.KestrunHost]$Server,
 57        [Parameter(Mandatory = $true)]
 58        [string]$Name,
 59        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 60        [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]$Options,
 61        [Parameter()]
 62        [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy,
 63        [Parameter(ParameterSetName = 'Items')]
 64        [switch] $SlidingExpiration,
 65        [Parameter(ParameterSetName = 'Items')]
 66        [string]$LoginPath,
 67        [Parameter(ParameterSetName = 'Items')]
 68        [string]$LogoutPath,
 69        [Parameter(ParameterSetName = 'Items')]
 70        [string]$AccessDeniedPath,
 71        [Parameter(ParameterSetName = 'Items')]
 72        [string]$ReturnUrlParameter,
 73        [Parameter(ParameterSetName = 'Items')]
 74        [timespan] $ExpireTimeSpan,
 75        [Parameter(ParameterSetName = 'Items', ValueFromPipeline = $true)]
 76        [Microsoft.AspNetCore.Http.CookieBuilder]$Cookie,
 77        [Parameter()]
 78        [switch]$PassThru
 79    )
 80    process {
 081        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 082            $Options = [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]::new()
 083            if ($PSBoundParameters.ContainsKey('SlidingExpiration')) { $Options.SlidingExpiration = $SlidingExpiration.I
 084            if ($PSBoundParameters.ContainsKey('LoginPath')) { $Options.LoginPath = $LoginPath }
 085            if ($PSBoundParameters.ContainsKey('LogoutPath')) { $Options.LogoutPath = $LogoutPath }
 086            if ($PSBoundParameters.ContainsKey('AccessDeniedPath')) { $Options.AccessDeniedPath = $AccessDeniedPath }
 087            if ($PSBoundParameters.ContainsKey('ReturnUrlParameter')) { $Options.ReturnUrlParameter = $ReturnUrlParamete
 088            if ($PSBoundParameters.ContainsKey('ExpireTimeSpan')) { $Options.ExpireTimeSpan = $ExpireTimeSpan }
 089            if ($PSBoundParameters.ContainsKey('Cookie')) { $Options.Cookie = $Cookie }
 90        }
 91        # Ensure the server instance is resolved
 092        $Server = Resolve-KestrunServer -Server $Server
 93
 094        [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddCookieAuthentication(
 095            $Server, $Name, $Options, $ClaimPolicy) | Out-Null
 096        if ($PassThru.IsPresent) {
 97            # if the PassThru switch is specified, return the modified server instance
 098            return $Server
 99        }
 100    }
 101}
 102

Methods/Properties

Add-KrCookiesAuthentication()