如何在 IronPDF 中使用 Cookie

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

查克尼特·宾

就网络技术而言,Cookie 是网站存储在用户计算机或设备上的小块数据。它们有各种用途,包括会话管理(帮助用户保持登录状态)、跟踪和分析(收集用户行为数据以改进网站)。然而,cookie 的使用引发了有关隐私的讨论,导致了 GDPR 和 CCPA 等法规的出台,现代网络浏览器为用户提供了对 cookie 管理的控制,以解决这些问题。


适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronPDFNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。

适用于PDF的C# NuGet库 nuget.org/packages/IronPdf/
Install-Package IronPdf

考虑安装 IronPDF DLL 直接。下载并手动安装到您的项目或GAC表单中: IronPdf.zip

手动安装到你的项目中

下载DLL

在使用该方法应用 Cookie 之前,请将 RequestContext 属性设置为 RequestContexts.Global。然后,创建 ChromeHttpLoginCredentials 类并将其传递给 ApplyCookies 方法。现在,呈现器已准备就绪,可用于将 HTML 内容呈现为带有 Cookie 的 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)
VB   C#

RequestContexts 枚举:该枚举定义了浏览器请求上下文,用于建立单个渲染之间的关系。它对于管理 cookie 和用户偏好至关重要。

  • 隔离:创建一个新的请求上下文,该上下文与之前或未来的渲染隔离。建议用于确保当前渲染不受之前渲染的影响。
  • 全局:使用全局请求上下文,该上下文在所有渲染中共享。在某些情况下,这对在渲染之间持续保持某些浏览器状态非常有用。

  • 自动:默认为 IronPdf.Rendering.RequestContexts.Isolated,但如果用户曾调用 IronPdf.ChromePdfRenderer.ApplyCookies 则会切换为 IronPdf.Rendering.RequestContexts.Global(System.String, IronPdf.ChromeHttpLoginCredentials).

在请求中使用自定义 Cookie 需要设置 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)
VB   C#

查克尼特·宾

软件工程师

Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。