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

如何在 C# 中使用 IronPDF 的 Cookie#.

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

IronPDF使用ApplyCookies方法将cookies集成到PDF渲染中,在HTML到PDF转换期间维护会话信息和用户身份验证。

Cookie 是网站存储在用户设备上的小数据。 这些工具可以管理会话、跟踪用户行为并存储偏好。 GDPR 和 CCPA 等隐私法规加强了对 cookie 管理的关注,促使浏览器为用户提供对 cookie 处理的更大控制。 using IronPDF 的 Chrome 浏览器渲染引擎时,cookie 会在 HTML 到 PDF 的转换过程中保持状态。 在呈现需要 TLS 网站验证和系统登录或用户特定偏好的页面时,这一点至关重要。

快速入门:在IronPDF中使用 Cookie using IronPDF 将 cookie 整合到 PDF 渲染流程中。 本指南演示了使用IronPdf API在HTML到PDF转换中管理cookies。 使用ApplyCookies方法应用标准或自定义cookies,所需代码最少。

  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属性?

在应用cookies之前,将RequestContexts.Global。 创建ApplyCookies方法。 然后,渲染器通过 Cookie 将 HTML 内容渲染为 PDF。

RequestContext属性与HTTP请求头和身份验证一起工作。 它确定了渲染会话之间的 cookie 共享,这对应用程序在多代 PDF 中保持会话状态至关重要。

如何使用ApplyCookies方法?

using 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 应用程序转换为 PDF 时,这种方法都能发挥作用。

我应该选择哪个RequestContext

RequestContexts 枚举定义了浏览器请求上下文,用于建立渲染之间的关系。 它可以管理 cookie 和用户偏好。

  • Isolated:创建新的、隔离的请求上下文。 防止当前渲染受之前渲染的影响。 适用于多线程 PDF 生成
  • Global:使用所有渲染间共享的全局请求上下文。 在渲染之间保持浏览器状态。 非常适合在 PDF 操作中维护会话数据。
  • Auto:默认使用 IronPdf.Rendering.RequestContexts.Isolated。 如果调用了 IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials),则切换到 IronPdf.Rendering.RequestContexts.Global

Blazor Server应用程序中实现cookie时,选择适当的RequestContext以在服务器端渲染之间维护正确的会话状态。


Icon Quote related to 我应该选择哪个RequestContext?

我最喜欢的这种库是 IronPDF。它允许快速高效地操作 PDF 文件。它还具有许多有价值的功能,比如导出为 PDF/A 格式和数字签名 PDF 文档。

Milan Jovanovic related to 我应该选择哪个RequestContext?

Milan Jovanovic

微软MVP

查看案例研究
Icon Quote related to 我应该选择哪个RequestContext?

IronOCR 意味着我们每年可以节省 $40,000 的人工处理成本,同时提高生产力,并释放资源用于高影响任务。我强烈推荐它。

Brent Matzelle related to 我应该选择哪个RequestContext?

Brent Matzelle

首席技术官,OPYN

查看案例研究
Icon Quote related to 我应该选择哪个RequestContext?

Iron Suite 在我们的运营中起着至关重要的作用。这些工具提高了业务各方面的效率,包括创建平面图和改善库存管理。

David Jones related to 我应该选择哪个RequestContext?

David Jones

首席软件工程师,Agorus Build

查看案例研究

如何应用自定义 Cookie?

什么是 IronPDF 中的自定义 Cookie?

CustomCookies属性。 该属性接受字符串键值对字典。 自定义 cookie 可以处理复杂的身份验证系统,或在渲染过程中传递应用级数据。

自定义 cookie 与标准 HTTP cookie 不同,允许定义任何键值对。 这种灵活性适合使用 JWT 标记、会话 ID 或自定义身份验证机制的现代网络应用程序。

如何实施自定义 Cookie?

using 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

这种方法适合重 JavaScript 网站依赖 Cookie 进行状态管理或实施自定义日志记录解决方案

自定义 cookie 可处理标准 HTTP 凭证无法管理的特定会话数据或身份验证令牌。 将它们与自定义验证系统一起使用,或在 PDF 生成过程中维护用户首选项。

标准cookie(通过ApplyCookies方法)适用于:

  • 基本 HTTP 身份验证
  • Windows 身份验证环境
  • 简单的会话管理

自定义 cookie excel 用于:

  • 基于 JWT 令牌的身份验证
  • 带有多个参数的复杂会话管理
  • 第三方身份验证提供程序(OAuth、SAML)
  • 用户偏好和设置维护
  • 分析和跟踪要求

常见问题包括使用Isolated上下文时cookie未在渲染之间持久化、由于cookie值错误导致身份验证失败、和在PDF生成前cookie过期的定时问题。 验证cookie的有效性,并考虑使用Global上下文来实现持久会话。

故障排除提示:

1.Cookie过期:验证 cookie 是否过期。 为短期代币实施刷新逻辑。 2.域限制:确保 cookie 域与呈现的 URL 匹配。 3.安全 Cookie:在呈现 HTTPS URL 时正确配置安全 Cookie。 4.同一网站政策:考虑影响跨源请求的浏览器 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 属性自定义 cookies。只需创建一个包含 Cookie 键值对的 dictionary,并将其赋值给 ChromePdfRenderer 的 RenderingOptions.CustomCookies 属性即可。

如何将受 cookie 保护的页面渲染为 PDF?

using IronPDF 的 ChromePdfRenderer,将 RequestContext 设置为 Global,并通过 CustomCookies 属性添加 cookie。然后使用受保护的 URL 调用 RenderUrlAsPdf()。渲染器会在请求中包含 cookie,从而允许访问受保护的内容。

在 PDF 渲染过程中,cookie 可以处理哪些类型的身份验证?

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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 20,296,129 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。