Modifier

Partager via


Cookie API overview

This article provides an overview of the application programming interfaces (APIs) in the Microsoft Dynamics 365 Commerce online software development kit (SDK) that are used to set and get cookie data.

Before cookies can be stored, the user must give consent. The Dynamics 365 Commerce online SDK provides utilities that help guarantee that the read/write operation of a cookie depends on user consent.

The Dynamics 365 Commerce online SDK provides a set of APIs that access cookies from within the props.context.request.cookies API, as shown in the following interface.

export interface ICookieContext {
    get<T>(cookieName: string): ICookieValue<T>;
    set<T>(cookieName: string, cookieValue: T, options?: ICookieSetOptions): void;
    getCartCookie(): string;
    setCartCookie(cart: Cart, isAuthenticated: boolean): void;
    getCheckoutCartCookie(): string;
    setCheckoutCartCookie(cart: Cart, isAuthenticated: boolean): void;
    removeCheckoutCartCookie(): void;
    remove(cookieName: string): void;
    isConsentGiven(): boolean;
    setConsentCookie(): void;
    deleteConsentCookie(): void;
    getTargetIdCookie(): string;
}

The setConsentCookie() API is used to obtain user consent before cookies can be written.

The isConsentGiven() API is used to determine whether user consent has been given.

The following example shows how to set a cookie.

this.props.context.request.cookies.set<string>('favoriteColor', 'blue');

If user consent isn't given before this API is called, the SDK maintains a queue of all set operations and sets a cookie only after the user gives consent.

The following example shows how to get the value of a cookie.

const favColor = this.props.context.request.cookies.get<string>('favoriteColor');

Additional resources

Request properties object

App settings

Platform settings file

Extend a module definition file

Interactive components overview

Mock the signed-in state during local development

Configure module properties to be shown based on context

Globalize modules by using the CultureInfoFormatter class

Set up Azure Key Vault for secure key management