如何在 C# 中使用 IronPDF 用 Cookies 安全地生成 PDF

如何在 C# 中使用IronPDF的 Cookie 進行 HTML轉PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF使用 RequestContext 屬性和 ApplyCookies 方法將 cookie 整合到 PDF 渲染中,在 HTML 到 PDF 轉換期間維護會話資訊和使用者驗證。

Cookie 是網站儲存在使用者裝置上的小型資料檔案。 它們管理會話、追蹤使用者行為並儲存偏好。 GDPR 和 CCPA 等隱私權法規更重視 cookie 管理,促使瀏覽器為使用者提供對 cookie 處理的更大控制權。

使用IronPDF 的 Chrome 渲染引擎時,cookie 在HTML 到 PDF 轉換過程中會保持狀態。 對於需要TLS 網站驗證、系統登入或使用者特定偏好設定的頁面渲染而言,這一點至關重要。

快速入門:在IronPDF中使用 Cookie

使用IronPDF將 cookie 整合到 PDF 渲染過程中。 本指南示範如何使用IronPDF API 在 HTML 到 PDF 轉換過程中管理 cookie。 使用 RequestContext 屬性和 ApplyCookies 方法,以最少的程式碼套用標準或自訂 cookie。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    new IronPdf.ChromePdfRenderer { RenderingOptions = { RequestContext = IronPdf.Rendering.RequestContexts.Global, CustomCookies = new Dictionary<string, string> { { "sessionId", "your_cookie_value" } } } }
        .RenderUrlAsPdf("https://example.com/protected")
        .SaveAs("secureWithCookies.pdf");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer


什麼是 RequestContext 屬性?

在套用 cookie 之前,請將RequestContext屬性設為 RequestContexts.Global。 建立 ChromeHttpLoginCredentials 類別並將其傳遞給 ApplyCookies 方法。 然後渲染器將帶有 cookie 的 HTML 內容渲染成 PDF。

RequestContext 屬性與HTTP 請求標頭和驗證有關。 它決定渲染會話之間的 cookie 共享,這對於在多個 PDF 生成過程中保持會話狀態的應用程式至關重要。

如何使用 ApplyCookies 方法?

使用IronPDF應用 cookie:

: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)
$vbLabelText   $csharpLabel

當將ASPX 頁面轉換為需要驗證的 PDF或使用ASP.NET MVC 應用程式時,此方法有效。

我該選擇哪個 RequestContext?

RequestContexts 枚舉定義了瀏覽器請求上下文,建立了渲染之間的關係。 它管理 cookie 和用戶偏好設定。

-隔離:建立新的、隔離的請求上下文。 防止目前渲染結果受到先前渲染結果的影響。 非常適合多線程PDF生成。 -全域:在所有渲染之間使用共享的全域請求上下文。 在渲染過程中保持瀏覽器狀態。 非常適合在 PDF 操作過程中維護會話資料。 -自動:預設為 IronPdf.Rendering.RequestContexts.Isolated。 如果呼叫了 IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials),則切換到 IronPdf.Rendering.RequestContexts.Global

Blazor Server 應用程式中實作 cookie 時,請選擇適當的 RequestContext 來維護伺服器端渲染之間的正確會話狀態。


如何套用自訂 Cookie?

自訂 Cookie 需要設定CustomCookies屬性。 此屬性接受字串鍵值對字典。 自訂 cookie 可處理複雜的身份驗證系統或在渲染期間傳遞應用程式層級的資料。

自訂 cookie 與標準 HTTP cookie 的區別在於,它允許定義任何鍵值對。 這種靈活性適用於使用 JWT 令牌、會話 ID 或自訂身份驗證機制的現代 Web 應用程式。

如何實作自訂 Cookie?

使用IronPDF應用自訂 cookie:

: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)
$vbLabelText   $csharpLabel

這種方法適用於依賴 cookie 進行狀態管理或實施自訂日誌記錄解決方案的 JavaScript 密集型網站

何時應該使用自訂 Cookie,何時應該使用標準 Cookie?

自訂 cookie 處理標準 HTTP 憑證無法管理的特定會話資料或驗證令牌。 可與自訂身份驗證系統搭配使用,或在產生 PDF 時保留使用者偏好設定。

標準 cookie(透過 ApplyCookies 方法)適用:

  • 基本 HTTP 驗證
  • Windows 驗證環境
  • 簡單的會話管理

自訂 cookie 的優勢:

  • 基於 JWT 令牌的身份驗證
  • 具有多個參數的複雜會話管理
  • 第三方身分驗證提供者(OAuth、SAML) 使用者偏好和設定維護 分析和追蹤要求

Cookie實施中常見的問題有哪些?

常見問題包括使用 Isolated 上下文時 cookie 無法在渲染之間保留、由於 cookie 值不正確而導致的身份驗證失敗,以及 cookie 在 PDF 生成之前過期的時間問題。 驗證 cookie 的有效性,並考慮持久會話的 Global 上下文。

故障排除提示:

  1. Cookie 過期:驗證 cookie 是否已過期。 實作短期令牌的刷新邏輯。 2.網域限制:確保 cookie 網域與渲染後的 URL 相符。 3.安全 Cookie :在渲染 HTTPS URL 時正確設定安全 Cookie。
  2. SameSite 策略:考慮影響跨網域請求的瀏覽器 SameSite cookie 策略。

有關高級身份驗證和 cookie 方案,請參閱渲染選項文檔,其中涵蓋所有可用的 PDF 生成自訂設定。

常見問題解答

將 HTML 呈現為 PDF 時,如何套用 cookies?

要使用 IronPDF 應用 Cookie,請在 ChromePdfRenderer 上設定 RequestContext 屬性為 RequestContexts.Global,然後與 ChromeHttpLoginCredentials 一起使用 ApplyCookies 方法。這可確保在 HTML 到 PDF 的轉換過程中,cookies 能正確傳輸。

RequestContext 屬性用來做什麼?

IronPDF 中的 RequestContext 屬性決定 Cookie 在渲染階段之間的共用方式。它可與 HTTP 請求標頭和驗證一起運作,因此對於需要在多個 PDF 世代中維持會話狀態的應用程式來說非常重要。

我可以在 PDF 呈現過程中加入自訂 cookies 嗎?

是的,IronPDF 允許透過 CustomCookies 屬性自訂 cookies。只需用您的 cookie 鍵值對建立一個 Dictionary,並將其指定給 ChromePdfRenderer 的 RenderingOptions.CustomCookies 屬性。

如何將受 cookie 保護的頁面呈現成 PDF?

使用 IronPDF 的 ChromePdfRenderer,並將 RequestContext 設定為 Global,然後透過 CustomCookies 屬性加入您的 cookies。然後使用受保護的 URL 來呼叫 RenderUrlAsPdf()。渲染器將在請求中包含 cookies,允許存取受保護的內容。

Cookie 在 PDF 呈現中可處理哪些類型的驗證?

IronPDF 的 cookie 整合功能支援各種認證情境,包括 TLS 網站認證、系統登入和基於會話的認證。這在轉換需要使用者驗證的 ASPX 頁面或 ASP.NET MVC 應用程式時特別有用。

為什麼將 HTML 轉換為 PDF 時 Cookie 很重要?

在使用 IronPDF 的 Chrome 渲染引擎將 HTML 轉換為 PDF 的過程中,Cookies 會保持狀態。它們對於渲染需要驗證的頁面、保留使用者特定的偏好設定,以及在整個轉換過程中維護會話資訊都是不可或缺的。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 18,120,209 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 變成 PDF。