How to use Cookies with IronPDF
Cookies, in the context of web technology, are small pieces of data that websites store on a user's computer or device. They serve various purposes, from session management, where they help keep users logged in, to tracking and analytics, collecting data on user behavior for website improvement. However, the use of cookies has sparked discussions on privacy, leading to regulations like the GDPR and CCPA, and modern web browsers offer users control over cookie management to address these concerns.
How to use Cookies with IronPDF
- Download IronPDF from NuGet
- Prepare the HTML content to render with custom cookies
- Configure the RequestContext property to enable the use of cookies
- Use the
ApplyCookies
method to apply cookies - Use the CustomCookies property to implement custom cookies
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLApply Cookies Example
Before using the method to apply cookies, set the RequestContext property to RequestContexts.Global. Then, create the ChromeHttpLoginCredentials class and pass it to the ApplyCookies
method. The renderer is now ready to be used for rendering HTML content to PDF with 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)
RequestContexts Enum: This enum defines browser request contexts used to establish relationships between individual renders. It's essential for managing cookies and user preferences.
- Isolated: Creates a new request context that is isolated from previous or future renders. Recommended to ensure that the current render is not affected by previous renders.
- Global: Uses the global request context, which is shared between all renders. Useful in some cases for persisting certain browser states between renders.
- Auto: Defaults to IronPdf.Rendering.RequestContexts.Isolated but switches to IronPdf.Rendering.RequestContexts.Global if the user has ever invoked IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials).
Apply Custom Cookies Example
Using custom cookies in a request requires setting the CustomCookies property. This property accepts a dictionary of key-value pairs, both as strings.
: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)