| | | 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 | | #> |
| | | 50 | | function 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 { |
| | 0 | 81 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 82 | | $Options = [Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions]::new() |
| | 0 | 83 | | if ($PSBoundParameters.ContainsKey('SlidingExpiration')) { $Options.SlidingExpiration = $SlidingExpiration.I |
| | 0 | 84 | | if ($PSBoundParameters.ContainsKey('LoginPath')) { $Options.LoginPath = $LoginPath } |
| | 0 | 85 | | if ($PSBoundParameters.ContainsKey('LogoutPath')) { $Options.LogoutPath = $LogoutPath } |
| | 0 | 86 | | if ($PSBoundParameters.ContainsKey('AccessDeniedPath')) { $Options.AccessDeniedPath = $AccessDeniedPath } |
| | 0 | 87 | | if ($PSBoundParameters.ContainsKey('ReturnUrlParameter')) { $Options.ReturnUrlParameter = $ReturnUrlParamete |
| | 0 | 88 | | if ($PSBoundParameters.ContainsKey('ExpireTimeSpan')) { $Options.ExpireTimeSpan = $ExpireTimeSpan } |
| | 0 | 89 | | if ($PSBoundParameters.ContainsKey('Cookie')) { $Options.Cookie = $Cookie } |
| | | 90 | | } |
| | | 91 | | # Ensure the server instance is resolved |
| | 0 | 92 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 93 | | |
| | 0 | 94 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddCookieAuthentication( |
| | 0 | 95 | | $Server, $Name, $Options, $ClaimPolicy) | Out-Null |
| | 0 | 96 | | if ($PassThru.IsPresent) { |
| | | 97 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 98 | | return $Server |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |