如何在 C# 中使用 IronPDF 與 Cookie 進行安全的 PDF 生成

如何在C#中使用Cookie與IronPDF

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

IronPDF使用ApplyCookies方法將Cookie整合到PDF渲染中,保持會話資訊和使用者驗證在HTML到PDF轉換期間。

Cookie是網站在使用者裝置上儲存的小資料片段。 它們管理會話,跟蹤使用者行為,並儲存偏好設置。 像GDPR和CCPA這樣的隱私法規增加了對Cookie管理的關注,促使瀏覽器提供給使用者更大的Cookie處理控制。

使用IronPDF的Chrome渲染引擎時,Cookie在HTML到PDF轉換過程中保持狀態。 這在渲染需要TLS網站認證和系統登入或使用者特定偏好設置的頁面時至關重要。

快速入門:在IronPDF中使用Cookie

將Cookie整合到您的PDF渲染過程中,使用IronPDF。 本指南演示如何使用IronPdf API在HTML到PDF轉換過程中管理Cookie。 使用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


如何將Cookie應用於PDF渲染?

什麼是RequestContext屬性?

在應用Cookie之前設定RequestContexts.Global。 建立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頁面轉換為PDFASP.NET MVC應用程式時效果良好。

我應該選擇哪個RequestContext

RequestContexts Enum定義了瀏覽器請求上下文,建立渲染之間的關係。 它管理Cookie和使用者偏好設置。

  • Isolated: 建立新的,隔離的請求上下文。 防止當前渲染受到先前渲染的影響。 適用於多執行緒PDF生成
  • Global: 在所有渲染之間使用共享的全域請求上下文。 在渲染之間保持瀏覽器狀態。 適合在PDF操作中保持會話資料。
  • Auto: 預設為IronPdf.Rendering.RequestContexts.Isolated。 如果IronPdf.Rendering.RequestContexts.Global

Blazor伺服器應用程式中實施Cookie時,選擇適合的RequestContext以在伺服器端渲染之間保持適當的會話狀態。


Icon Quote related to 我應該選擇哪個RequestContext?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 我應該選擇哪個RequestContext?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 我應該選擇哪個RequestContext?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 我應該選擇哪個RequestContext?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 我應該選擇哪個RequestContext?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 我應該選擇哪個RequestContext?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

如何應用自訂Cookie?

IronPDF中的自訂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。
  4. SameSite政策:考慮影響跨源請求的瀏覽器SameSite Cookie政策。

有關高級身份驗證和Cookie的場景,請參考渲染選項文件,涵蓋所有可用的PDF生成自訂設置。

常見問題

在將 HTML 轉為 PDF 時,該如何套用 Cookie?

若要透過 IronPDF 套用 Cookie,請將 ChromePdfRenderer 上的 RequestContext 屬性設定為 RequestContexts.Global,然後使用 ChromeHttpLoginCredentials 搭配 ApplyCookies 方法。此操作可確保在 HTML 轉 PDF 的轉換過程中,Cookie 能正確傳輸。

RequestContext 屬性用於什麼用途?

IronPDF 中的 RequestContext 屬性決定了 Cookie 在渲染會話間的共享方式。它與 HTTP 請求標頭及驗證機制協同運作,對於需要在多次 PDF 生成過程中維持會話狀態的應用程式而言至關重要。

在 PDF 渲染過程中,我可以新增自訂 Cookie 嗎?

是的,IronPDF 允許透過 CustomCookies 屬性自訂 Cookie。只需建立一個包含 Cookie 鍵值對的字典,並將其指派給 ChromePdfRenderer 的 RenderingOptions.CustomCookies 屬性即可。

如何將受 Cookie 保護的頁面渲染為 PDF?

請使用 IronPDF 的 ChromePdfRenderer,並將 RequestContext 設定為 Global,再透過 CustomCookies 屬性加入您的 Cookie。接著使用受保護的 URL 呼叫 RenderUrlAsPdf()。此渲染器會將 Cookie 包含在請求中,從而允許存取受保護的內容。

Cookie 在 PDF 渲染中能處理哪些型別的驗證?

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

在將 HTML 轉換為 PDF 時,Cookie 為何如此重要?

在使用 IronPDF 的 Chrome 渲染引擎進行 HTML 轉 PDF 過程中,Cookie 會用於維持狀態。它們對於渲染需要驗證的頁面、保留使用者特定偏好設定,以及在整個轉換過程中維持工作階段資訊至關重要。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 19,936,792 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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