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