< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
92%
Covered lines: 13
Uncovered lines: 1
Coverable lines: 14
Total lines: 97
Line coverage: 92.8%
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

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    .PARAMETER PassThru
 29        If specified, the cmdlet returns the modified server instance after configuration.
 30    .EXAMPLE
 31        Add-KrCookiesAuthentication -Server $myServer -Name 'MyCookieAuth' -Options $myCookieOptions -ClaimPolicy $myCla
 32        Adds cookie authentication to the specified Kestrun server with the provided options and claim policy.
 33    .EXAMPLE
 34        Add-KrCookiesAuthentication -Name 'MyCookieAuth' -SlidingExpiration -LoginPath '/Login' -LogoutPath '/Logout' -A
 35        Configures cookie authentication with sliding expiration and custom paths for login, logout, and access denied
 36    .NOTES
 37        This cmdlet is part of the Kestrun PowerShell module and is used to configure cookie authentication for Kestrun 
 38    .LINK
 39        https://docs.kestrun.dev/docs/powershell/kestrun/authentication
 40#>
 41function Add-KrCookiesAuthentication {
 42    [KestrunRuntimeApi('Definition')]
 43    [CmdletBinding(defaultParameterSetName = 'Items')]
 44    [OutputType([Kestrun.Hosting.KestrunHost])]
 45    param(
 46        [Parameter(Mandatory = $false, ValueFromPipeline)]
 47        [Kestrun.Hosting.KestrunHost]$Server,
 48
 49        [Parameter(Mandatory = $true)]
 50        [string]$Name,
 51
 52        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 53        [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]$Options,
 54
 55        [Parameter()]
 56        [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy,
 57
 58        [Parameter(ParameterSetName = 'Items')]
 59        [switch] $SlidingExpiration,
 60        [Parameter(ParameterSetName = 'Items')]
 61        [string]$LoginPath,
 62        [Parameter(ParameterSetName = 'Items')]
 63        [string]$LogoutPath,
 64        [Parameter(ParameterSetName = 'Items')]
 65        [string]$AccessDeniedPath,
 66        [Parameter(ParameterSetName = 'Items')]
 67        [string]$ReturnUrlParameter,
 68        [Parameter(ParameterSetName = 'Items')]
 69        [timespan] $ExpireTimeSpan,
 70        [Microsoft.AspNetCore.Http.CookieBuilder]$Cookie,
 71        [Parameter()]
 72        [switch]$PassThru
 73    )
 74    process {
 175        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 176            $Options = [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]::new()
 277            if ($PSBoundParameters.ContainsKey('SlidingExpiration')) { $Options.SlidingExpiration = $SlidingExpiration.I
 278            if ($PSBoundParameters.ContainsKey('LoginPath')) { $Options.LoginPath = $LoginPath }
 279            if ($PSBoundParameters.ContainsKey('LogoutPath')) { $Options.LogoutPath = $LogoutPath }
 280            if ($PSBoundParameters.ContainsKey('AccessDeniedPath')) { $Options.AccessDeniedPath = $AccessDeniedPath }
 181            if ($PSBoundParameters.ContainsKey('ReturnUrlParameter')) { $Options.ReturnUrlParameter = $ReturnUrlParamete
 282            if ($PSBoundParameters.ContainsKey('ExpireTimeSpan')) { $Options.ExpireTimeSpan = $ExpireTimeSpan }
 283            if ($PSBoundParameters.ContainsKey('Cookie')) { $Options.Cookie = $Cookie }
 84        }
 85        # Ensure the server instance is resolved
 186        $Server = Resolve-KestrunServer -Server $Server
 87
 188        [Kestrun.Hosting.KestrunHostAuthExtensions]::AddCookieAuthentication(
 189            $Server, $Name, $Options, $ClaimPolicy) | Out-Null
 190        if ($PassThru.IsPresent) {
 91            # if the PassThru switch is specified, return the server instance
 92            # Return the modified server instance
 093            return $Server
 94        }
 95    }
 96}
 97

Methods/Properties

Add-KrCookiesAuthentication()