| | | 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 | | #> |
| | | 56 | | function 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 |
| | 0 | 99 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 100 | | # Build Options only when not provided directly |
| | 0 | 101 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 102 | | $Options = [Kestrun.Authentication.CookieAuthOptions]::new() |
| | | 103 | | # Set host reference |
| | 0 | 104 | | $Options.Host = $Server |
| | 0 | 105 | | if ($PSBoundParameters.ContainsKey('SlidingExpiration')) { $Options.SlidingExpiration = $SlidingExpiration.I |
| | 0 | 106 | | if ($PSBoundParameters.ContainsKey('LoginPath')) { $Options.LoginPath = $LoginPath } |
| | 0 | 107 | | if ($PSBoundParameters.ContainsKey('LogoutPath')) { $Options.LogoutPath = $LogoutPath } |
| | 0 | 108 | | if ($PSBoundParameters.ContainsKey('AccessDeniedPath')) { $Options.AccessDeniedPath = $AccessDeniedPath } |
| | 0 | 109 | | if ($PSBoundParameters.ContainsKey('ReturnUrlParameter')) { $Options.ReturnUrlParameter = $ReturnUrlParamete |
| | 0 | 110 | | if ($PSBoundParameters.ContainsKey('ExpireTimeSpan')) { $Options.ExpireTimeSpan = $ExpireTimeSpan } |
| | 0 | 111 | | if ($PSBoundParameters.ContainsKey('Cookie')) { $Options.Cookie = $Cookie } |
| | | 112 | | |
| | 0 | 113 | | if (-not ([string]::IsNullOrWhiteSpace($Description))) { |
| | 0 | 114 | | $Options.Description = $Description |
| | | 115 | | } |
| | | 116 | | # OpenAPI documentation IDs |
| | 0 | 117 | | $Options.DocumentationId = $DocId |
| | | 118 | | } |
| | | 119 | | # Add cookie authentication to the server |
| | 0 | 120 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddCookieAuthentication( |
| | 0 | 121 | | $Server, $AuthenticationScheme, $DisplayName, $Options, $ClaimPolicy) | Out-Null |
| | | 122 | | |
| | | 123 | | # Return the modified server instance if PassThru is specified |
| | 0 | 124 | | if ($PassThru.IsPresent) { |
| | 0 | 125 | | return $Server |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | } |