IronPDFでクッキーを使用する方法
ウェブ技術の文脈におけるクッキーは、ウェブサイトがユーザーのコンピュータやデバイスに保存する小さなデータの断片です。クッキーは、ユーザーのログイン状態を維持するセッション管理から、ユーザーの行動データを収集してウェブサイトの改善に役立てるトラッキングや分析まで、さまざまな目的に使用されます。 しかし、クッキーの使用はプライバシーに関する議論を引き起こし、GDPRやCCPAのような規制を生み出しました。また、現代のウェブブラウザはこれらの懸念に対処するため、ユーザーにクッキー管理の制御を提供しています。
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
IronPDFでクッキーを使用する方法
- NuGetからIronPdfをダウンロードする
- カスタムクッキーでレンダリングするためにHTMLコンテンツを準備する
- 構成する リクエストコンテキスト クッキーの使用を可能にするプロパティ
- 以下を使用
`ApplyCookies`
クッキーを適用するメソッド - 以下を使用 カスタムクッキー カスタムクッキーを実装するためのプロパティ
クッキーの適用例
メソッドを使用してクッキーを適用する前に、RequestContextプロパティをRequestContexts.Globalに設定します。 次に、ChromeHttpLoginCredentialsクラスを作成し、それをApplyCookies
メソッドに渡します。 レンダラーは、クッキーを使用してHTMLコンテンツをPDFにレンダリングするために使用できるようになりました。
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-cookies.cs
using IronPdf;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;
ChromeHttpLoginCredentials credentials = new ChromeHttpLoginCredentials() {
NetworkUsername = "testUser",
NetworkPassword = "testPassword"
};
string uri = "http://localhost:51169/Invoice";
// Apply cookies
renderer.ApplyCookies(uri, credentials);
Imports IronPdf
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global
Dim credentials As New ChromeHttpLoginCredentials() With {
.NetworkUsername = "testUser",
.NetworkPassword = "testPassword"
}
Dim uri As String = "http://localhost:51169/Invoice"
' Apply cookies
renderer.ApplyCookies(uri, credentials)
RequestContexts Enum: この列挙型は、個々のレンダリング間の関係を確立するために使用されるブラウザリクエストコンテキストを定義します。 それは、クッキーやユーザープリファレンスを管理するために不可欠です。
- 隔離:以前または将来のレンダリングから分離された新しいリクエストコンテキストを作成します。 現在のレンダリングが以前のレンダリングによって影響を受けないようにすることを推奨します。
- グローバル: すべてのレンダー間で共有されるグローバルリクエストコンテキストを使用します。 レンダリング間で特定のブラウザー状態を永続化するためにいくつかのケースで役立ちます。
- 自動: デフォルトはIronPdf.Rendering.RequestContexts.Isolatedですが、ユーザが一度でもIronPdf.ChromePdfRenderer.ApplyCookiesを呼び出した場合にはIronPdf.Rendering.RequestContexts.Globalに切り替わります(System.String、IronPdf.ChromeHttpLoginCredentials).
カスタムクッキーを適用する例
リクエストでカスタムクッキーを使用するには、CustomCookies プロパティを設定する必要があります。 このプロパティは、キーと値の両方を文字列として持つ辞書を受け入れます。
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-custom-cookies.cs
using IronPdf;
using System;
using System.Collections.Generic;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
Dictionary<string, string> customCookies = new Dictionary<string, string>();
// Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies;
var uri = new Uri("https://localhost:44362/invoice");
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);
Imports IronPdf
Imports System
Imports System.Collections.Generic
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private customCookies As New Dictionary(Of String, String)()
' Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies
Dim uri As New Uri("https://localhost:44362/invoice")
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf(uri)