SameSite and security
Pick the attributes, set the cookie, and check what the browser stored. Choosing sameSite: 'None' or partitioned with secure unchecked shows how CookieService turns Secure on for you (a warning appears in the browser console).
import { ChangeDetectionStrategy, Component, inject, signal, WritableSignal } from '@angular/core';import { CookieService, SameSite } from 'ngx-cookie-service';import { CookieInspectorComponent } from '../shared/cookie-inspector.component';
@Component({ selector: 'same-site-example', templateUrl: './same-site-example.component.html', imports: [CookieInspectorComponent], changeDetection: ChangeDetectionStrategy.OnPush,})export class SameSiteExampleComponent { private readonly cookieService: CookieService = inject(CookieService);
protected readonly sameSiteValues: SameSite[] = ['Lax', 'Strict', 'None']; protected readonly sameSite: WritableSignal<SameSite> = signal('Lax'); protected readonly secure: WritableSignal<boolean> = signal(false); protected readonly partitioned: WritableSignal<boolean> = signal(false); protected readonly refresh: WritableSignal<number> = signal(0);
protected call(): string { const options = [`sameSite: '${this.sameSite()}'`, `secure: ${this.secure()}`, `partitioned: ${this.partitioned()}`, `path: '/'`]; return `cookieService.set('docs-same-site', 'yes', { ${options.join(', ')} })`; }
protected set(): void { // sameSite: 'None' and partitioned: true both need Secure, so CookieService turns it on and logs a console warning. this.cookieService.set('docs-same-site', 'yes', { sameSite: this.sameSite(), secure: this.secure(), partitioned: this.partitioned(), path: '/', }); this.refresh.update((r) => r + 1); }}<div class="docs-row"> <div class="docs-field"> <label for="same-site-value">sameSite</label> <select id="same-site-value" [value]="sameSite()" (change)="sameSite.set($any($event.target).value)"> @for (value of sameSiteValues; track value) { <option [value]="value">{{ value }}</option> } </select> </div> <label class="docs-check"> <input type="checkbox" [checked]="secure()" (change)="secure.set($any($event.target).checked)" /> secure </label> <label class="docs-check"> <input type="checkbox" [checked]="partitioned()" (change)="partitioned.set($any($event.target).checked)" /> partitioned </label></div><pre class="docs-output">{{ call() }}</pre><div class="docs-actions"> <button type="button" class="primary" (click)="set()">Set cookie</button></div>
<h4 class="docs-subheading">What the browser stored</h4><docs-cookie-inspector name="docs-same-site" [refresh]="refresh()" />Attributes
Section titled “Attributes”| Option | What it does |
|---|---|
sameSite: 'Lax' |
Default. Sent on same-site requests and on top-level navigations from other sites. |
sameSite: 'Strict' |
Only sent on same-site requests. Following a link from another site arrives without it. |
sameSite: 'None' |
Also sent on cross-site requests, such as iframes and fetch from another site. Requires Secure. |
secure: true |
Only sent over HTTPS (browsers also allow it on http://localhost). |
partitioned: true |
Stores the cookie separately for each top-level site (CHIPS). Requires Secure. |
- The stored attributes are read with the browser’s Cookie Store API, because
document.cookieonly exposes names and values. That inspector is part of this site, not of the library. - To delete a partitioned cookie, pass
partitioned: truetodelete()as well. HttpOnlycookies can only be set by a server. See Server-side rendering.