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

How to use Cookies with IronPDF

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

Cookies在網絡技術環境中,是網站存儲在用戶計算機或裝置上的小型數據片段。它們具有多種用途,從會話管理(幫助用戶保持登錄狀態)到跟蹤和分析,用於收集用戶行為數據以改善網站。 然而,Cookies的使用引發了關於隱私的討論,導致了像GDPR和CCPA這樣的法規,現代網絡瀏覽器提供用戶控制Cookies管理的功能,以解決這些問題。

快速入門:在IronPDF中使用Cookies

使用IronPDF輕鬆整合Cookies到您的PDF渲染過程中。 這份快速指南向您展示如何使用IronPDF API 管理Cookies,確保您的HTML到PDF轉換保持會話信息和用戶設置。 只需幾行代碼,開發者就可以應用標準或自定義的Cookies,通過RequestContext屬性和ApplyCookies方法進行無縫整合。 立即開始增強您的PDF文件!

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    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. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5步驟)

  1. 從NuGet下載IronPDF
  2. 準備要用自定義Cookies渲染的HTML內容
  3. 配置RequestContext屬性以啟用Cookies的使用
  4. 使用ApplyCookies方法應用Cookies
  5. 使用CustomCookies屬性實現自定義Cookies


應用Cookies示例

在使用方法應用Cookies之前,將RequestContext屬性設置為RequestContexts.Global。 然後,創建ChromeHttpLoginCredentials類並將其傳遞給ApplyCookies方法。 渲染器現在已準備好用於使用Cookies渲染HTML內容到PDF。

以下是使用IronPDF應用Cookies的示例:

: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

RequestContexts枚舉:這個枚舉定義了瀏覽器請求上下文,用於建立個別渲染之間的關係。 這對管理Cookies和用戶偏好至關重要。

  • Isolated:創建一個從先前或未來渲染隔離的新請求上下文。 建議確保當前渲染不受先前渲染影響。
  • Global:使用全局請求上下文,這在所有渲染之間共享。 在某些情況下,這對持續某些瀏覽器狀態之間的渲染非常有用。
  • Auto:默認為IronPdf.Rendering.RequestContexts.Isolated,但如果用戶曾經調用過IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials),則切換到IronPdf.Rendering.RequestContexts.Global

應用自定義Cookies示例

在請求中使用自定義Cookies需要設置CustomCookies屬性。 此屬性接受一個鍵值對的字典,兩者都是字符串。

以下是使用IronPDF應用自定義Cookies的示例:

: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

常見問題解答

如何在C#中將受Cookies保護的網頁渲染為PDF?

您可以透過配置RequestContext為RequestContexts.Global,並使用CustomCookies屬性指定Cookies,使用IronPDF將受Cookies保護的網頁渲染為PDF。然後,調用RenderUrlAsPdf方法,傳入網頁的URL。

RequestContext屬性在IronPDF中的作用是什麼?

在IronPDF中,RequestContext屬性決定在PDF渲染期間如何管理Cookies和瀏覽器狀態。它可以設置為Isolated、Global或Auto來控制渲染間狀態的持久性。

如何在將HTML渲染為PDF時應用自訂Cookies?

要在IronPDF中應用自訂Cookies,您需要以鍵值對字典形式設置CustomCookies屬性,這些Cookies將在HTML到PDF渲染過程中應用。

IronPDF中用來管理Cookies的方法有哪些?

IronPDF使用ApplyCookies等方法和CustomCookies等屬性來管理Cookies。這些工具讓您能把會話或自訂Cookies整合到PDF渲染工作流程中。

為什麼在PDF渲染中管理Cookies很重要?

在PDF渲染中管理Cookies對於保持用戶會話、啟用身份驗證,以及確保渲染內容反映用戶特定數據和偏好來說至關重要。它對符合隱私法規如GDPR和CCPA也很重要。

ChromeHttpLoginCredentials 類如何在Cookies管理中提供幫助?

IronPDF中的ChromeHttpLoginCredentials類用於在應用Cookies時傳遞登入憑證,從而在PDF渲染過程中啟用會話身份驗證。

IronPDF中不同類型的RequestContexts有哪些?

IronPDF提供了三種類型的RequestContexts:Isolated(為每次渲染創建一個新的上下文)、Global(在渲染間共享上下文)和Auto(根據先前的Cookie應用切換)。

如何開始使用IronPDF進行Cookies管理及PDF渲染?

要開始使用IronPDF進行Cookies管理及PDF渲染,從NuGet下載庫,準備好您的HTML內容,配置RequestContext屬性,以及根據需求利用ApplyCookies和CustomCookies方法。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布