< Summary - Kestrun — Combined Coverage

Information
Class: Public.Cookies.Invoke-KrCookieSignOut
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Cookies/Invoke-KrCookieSignOut.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 130
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/12/2025 - 03:43:11 Line coverage: 0% (0/12) Total lines: 55 Tag: Kestrun/Kestrun@d160286e3020330b1eb862d66a37db2e26fc904209/16/2025 - 04:01:29 Line coverage: 0% (0/17) Total lines: 65 Tag: Kestrun/Kestrun@e5263347b0baba68d9fd62ffbf60a7dd87f994bb11/19/2025 - 02:25:56 Line coverage: 0% (0/36) Total lines: 131 Tag: Kestrun/Kestrun@98ff905e5605a920343154665980a71211a03c6d12/12/2025 - 17:27:19 Line coverage: 0% (0/36) Total lines: 130 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Cookies/Invoke-KrCookieSignOut.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Signs out the current user by removing their authentication cookie for the given scheme.
 4.DESCRIPTION
 5    Wraps SignOutAsync on the current HTTP context to remove a cookie-based session.
 6    Designed for use inside Kestrun route script blocks where $Context is available.
 7
 8    For OIDC logout, use -OidcScheme to sign out from both the cookie and OIDC provider.
 9    This will redirect to the OIDC provider's logout endpoint automatically.
 10.PARAMETER Scheme
 11    Authentication scheme to use (default 'Cookies').
 12.PARAMETER AuthKind
 13    Authentication kind: 'Cookies' (default), 'OAuth2', or 'Oidc'.
 14    Use 'OAuth2' to sign out from both Cookies and OAuth2 schemes.
 15    Use 'Oidc' to sign out from both Cookies and OIDC schemes (triggers redirect to IdP logout).
 16.PARAMETER Redirect
 17    If specified, redirects the user to the login path after signing out.
 18    If the login path is not configured, redirects to '/'.
 19    NOTE: This is ignored when OidcScheme is used, as the OIDC handler manages the redirect.
 20.PARAMETER RedirectUri
 21    URI to redirect to after OIDC logout completes (default '/').
 22    Only used when OidcScheme is specified.
 23.PARAMETER Properties
 24    Additional sign-out authentication properties to pass to the SignOut call.
 25.PARAMETER WhatIf
 26    Shows what would happen if the command runs. The command is not run.
 27.PARAMETER Confirm
 28    Prompts you for confirmation before running the command. The command is not run unless you respond
 29    affirmatively.
 30.EXAMPLE
 31    Invoke-KrCookieSignOut  # Signs out the current user from the default 'Cookies' scheme.
 32.EXAMPLE
 33    Invoke-KrCookieSignOut -Scheme 'MyCookieScheme'  # Signs out the current user from the specified scheme.
 34.EXAMPLE
 35    Invoke-KrCookieSignOut -OidcScheme 'oidc' -RedirectUri '/'  # Signs out from both Cookies and OIDC, redirects to roo
 36.OUTPUTS
 37    None
 38#>
 39function Invoke-KrCookieSignOut {
 40    [KestrunRuntimeApi('Route')]
 41    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low', DefaultParameterSetName = 'SimpleIdentity')]
 42    [OutputType([void])]
 43    param(
 44        [Parameter()]
 45        [string]$Scheme = 'Cookies',
 46
 47        [Parameter()]
 48        [ValidateSet('OAuth2', 'Oidc', 'Cookies')]
 49        [string]$AuthKind = 'Cookies',
 50
 51        [switch]$Redirect,
 52
 53        [Parameter()]
 54        [string]$RedirectUri = '/',
 55
 56        [hashtable]$Properties
 57    )
 58    # Only works inside a route script block where $Context is available
 059    if ($null -ne $Context -and $null -ne $KrServer) {
 060        if ($PSCmdlet.ShouldProcess($Scheme, 'SignOut')) {
 61
 062            switch ($AuthKind) {
 63                'OAuth2' {
 64                    # OAuth2 logout requires special handling
 065                    Write-KrLog -Level Information -Message 'Signing out from Cookie and OAuth2 ({oauth2Scheme}) schemes
 066                    $cookieSchemeName = $KrServer.RegisteredAuthentications.ResolveAuthenticationSchemeName($Scheme, $Au
 067                    Write-KrLog -Level Debug -Message 'Resolved Cookie scheme name: {scheme}' -Values $cookieSchemeName
 68
 69                    # Sign out from Cookie
 070                    $oidcProperties = [Microsoft.AspNetCore.Authentication.AuthenticationProperties]::new()
 071                    if (-not [string]::IsNullOrEmpty($RedirectUri)  ) {
 072                        $oidcProperties.RedirectUri = $RedirectUri
 73                    }
 074                    $Context.SignOut($cookieSchemeName, $oidcProperties) | Out-Null
 75
 076                    Write-KrLog -Level Information -Message 'OAuth2 logout initiated, OAuth2 handler will redirect to Id
 77                    return
 78                }
 79                'Oidc' {
 80
 81                    # OIDC logout requires special handling
 082                    Write-KrLog -Level Information -Message 'Signing out from Cookie ({cookieScheme}) and OIDC ({oidcSch
 083                    $schemeName = $KrServer.RegisteredAuthentications.ResolveAuthenticationSchemeName($Scheme, $AuthKind
 084                    Write-KrLog -Level Debug -Message 'Resolved OIDC scheme name: {scheme}' -Values $schemeName
 85
 086                    $Context.SignOut($schemeName) | Out-Null
 087                    $oidcProperties = [Microsoft.AspNetCore.Authentication.AuthenticationProperties]::new()
 088                    if (-not [string]::IsNullOrEmpty($RedirectUri)) {
 089                        $oidcProperties.RedirectUri = $RedirectUri
 90                    }
 091                    $Context.SignOut($Scheme, $oidcProperties) | Out-Null
 92
 093                    Write-KrLog -Level Information -Message 'OIDC logout initiated, OIDC handler will redirect to IdP lo
 94                    return
 95                }
 96                'Cookies' {
 097                    Write-KrLog -Level Information -Message 'Signing out from Cookie scheme: {scheme}' -Values $Scheme
 98
 99                    # Standard cookie-only logout
 0100                    if ($Context.User -and $Context.User.Identity.IsAuthenticated) {
 0101                        $Context.SignOut($Scheme, $Properties)
 102                    }
 103
 0104                    if ($Redirect) {
 0105                        $cookiesAuth = $null
 0106                        if ($KrServer.RegisteredAuthentications.Exists($Scheme, 'Cookie')) {
 0107                            $cookiesAuth = $KrServer.RegisteredAuthentications.Get($Scheme, 'Cookie')
 108                        } else {
 0109                            Write-KrLog -Level Warning -Message 'Authentication scheme {scheme} not found in registered 
 0110                            Write-KrErrorResponse -Message "Authentication scheme '$Scheme' not found." -StatusCode 400
 111                            return
 112                        }
 0113                        Write-KrLog -Level Information -Message 'User {@user} signed out from {scheme} authentication.' 
 114                        # Redirect to login path or root
 115
 0116                        if ($null -ne $cookiesAuth -and $cookiesAuth.LoginPath -and $cookiesAuth.LoginPath.ToString().Tr
 0117                            $url = $cookiesAuth.LoginPath
 118                        } else {
 0119                            $url = '/'
 120                        }
 0121                        Write-KrLog -Level Information -Message 'Redirecting {user} after logout to {path}' -Values $Con
 0122                        Write-KrRedirectResponse -Url $url
 123                    }
 124                }
 125            }
 126        }
 127    } else {
 0128        Write-KrOutsideRouteWarning
 129    }
 130}

Methods/Properties

Invoke-KrCookieSignOut()