Table of Contents

Class CookieAccessTokenProvider

Namespace
Keycloak.AuthServices.Authorization.AuthorizationServer
Assembly
Keycloak.AuthServices.Authorization.dll

IKeycloakAccessTokenProvider implementation for OIDC+Cookie Web Apps. Retrieves the access token stored in the cookie authentication scheme when SaveTokens = true is configured on the OpenIdConnect options.

public class CookieAccessTokenProvider : IKeycloakAccessTokenProvider
Inheritance
CookieAccessTokenProvider
Implements
Inherited Members

Remarks

Register this provider instead of (or before) calling AddAuthorizationServer when using AddKeycloakWebAppAuthentication:

services.AddCookieAccessTokenProvider();
services.AddKeycloakAuthorization().AddAuthorizationServer(...);

Ensure SaveTokens = true is set in your OIDC options so the token is persisted in the cookie:

.AddKeycloakWebApp(config, configureOpenIdConnectOptions: o =>
{
    o.SaveTokens = true;
    o.ResponseType = OpenIdConnectResponseType.Code;
});

Constructors

CookieAccessTokenProvider(IHttpContextAccessor, ILogger<CookieAccessTokenProvider>, string, string)

Initializes a new instance of the CookieAccessTokenProvider class.

public CookieAccessTokenProvider(IHttpContextAccessor contextAccessor, ILogger<CookieAccessTokenProvider> logger, string cookieScheme = "Cookies", string tokenName = "access_token")

Parameters

contextAccessor IHttpContextAccessor

The HTTP context accessor.

logger ILogger<CookieAccessTokenProvider>

The logger.

cookieScheme string

The cookie authentication scheme where tokens are stored. Defaults to AuthenticationScheme ("Cookies").

tokenName string

The name of the token to retrieve. Defaults to "access_token".

Properties

CookieScheme

Gets the cookie authentication scheme name used for token retrieval.

public string CookieScheme { get; }

Property Value

string

Methods

GetAccessTokenAsync(CancellationToken)

Gets the current access token.

public Task<string?> GetAccessTokenAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The cancellation token.

Returns

Task<string>

The access token, or null if no token is available.