IRONSOFTWAREHOME

How to use Cookies with IronPDF in C#

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronPDF uses the RequestContext property and ApplyCookies method to integrate cookies into PDF rendering, maintaining session information and user authentication during HTML-to-PDF conversions.

Cookies are small data pieces that websites store on user devices. They manage sessions, track user behavior, and store preferences. Privacy regulations like GDPR and CCPA have increased focus on cookie management, prompting browsers to offer users greater control over cookie handling.

When using IronPDF's Chrome rendering engine, cookies maintain state during the HTML to PDF conversion process. This is essential when rendering pages that require TLS website authentication and system logins or user-specific preferences.

Quickstart: Using Cookies in IronPDF

Integrate cookies into your PDF rendering process with IronPDF. This guide demonstrates using the IronPdf API to manage cookies during HTML-to-PDF conversions. Apply standard or custom cookies using the RequestContext property and ApplyCookies method with minimal code.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy 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");
    C#
  3. 3Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

How Do I Apply Cookies to PDF Rendering?

What Is the RequestContext Property?

Set the RequestContext property to RequestContexts.Global before applying cookies. Create the ChromeHttpLoginCredentials class and pass it to the ApplyCookies method. The renderer then renders HTML content to PDF with cookies.

The RequestContext property works with HTTP request headers and authentication. It determines cookie sharing between rendering sessions, critical for applications maintaining session state across multiple PDF generations.

How Do I Use the ApplyCookies Method?

Apply cookies using IronPDF:

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);

This approach works when converting ASPX pages to PDF requiring authentication or with ASP.NET MVC applications.

Which RequestContext Should I Choose?

RequestContexts Enum defines browser request contexts establishing relationships between renders. It manages cookies and user preferences.

  • Isolated: Creates new, isolated request contexts. Prevents current renders from being affected by previous renders. Ideal for multi-threaded PDF generation.
  • Global: Uses shared global request context between all renders. Persists browser states between renders. Perfect for maintaining session data across PDF operations.
  • Auto: Defaults to IronPdf.Rendering.RequestContexts.Isolated. Switches to IronPdf.Rendering.RequestContexts.Global if IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials) was invoked.

When implementing cookies in Blazor Server applications, choose the appropriate RequestContext to maintain proper session state between server-side renders.


My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.

David Jones

Lead Software Engineer, Agorus Build

View case study

How Do I Apply Custom Cookies?

What Are Custom Cookies in IronPDF?

CustomCookies require setting the CustomCookies property. This property accepts string key-value pair dictionaries. Custom cookies handle complex authentication systems or pass application-level data during rendering.

Custom cookies differ from standard HTTP cookies by allowing any key-value pair definition. This flexibility suits modern web applications using JWT tokens, session IDs, or custom authentication mechanisms.

How Do I Implement Custom Cookies?

Apply custom cookies using IronPDF:

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);

This approach suits JavaScript-heavy sites relying on cookies for state management or implementing custom logging solutions.

When Should I Use Custom Cookies vs Standard Cookies?

Custom cookies handle specific session data or authentication tokens not managed by standard HTTP credentials. Use them with custom authentication systems or to maintain user preferences during PDF generation.

Standard cookies (via ApplyCookies method) suit:

  • Basic HTTP authentication
  • Windows authentication environments
  • Simple session management

Custom cookies excel for:

  • JWT token-based authentication
  • Complex session management with multiple parameters
  • Third-party authentication providers (OAuth, SAML)
  • User preferences and settings maintenance
  • Analytics and tracking requirements

Common issues include cookies not persisting between renders using Isolated context, authentication failures from incorrect cookie values, and timing issues where cookies expire before PDF generation. Verify cookie validity and consider Global context for persistent sessions.

Troubleshooting tips:

  1. Cookie Expiration: Verify cookies haven't expired. Implement refresh logic for short-lived tokens.
  2. Domain Restrictions: Ensure cookie domains match the rendered URL.
  3. Secure Cookies: Configure secure cookies properly when rendering HTTPS URLs.
  4. SameSite Policies: Consider browser SameSite cookie policies affecting cross-origin requests.

For advanced authentication and cookie scenarios, see the rendering options documentation covering all available PDF generation customization settings.

Frequently Asked Questions

How does IronPDF handle cookies in PDF rendering?

IronPDF uses the `RequestContext` property and `ApplyCookies` method to integrate cookies into PDF rendering, maintaining session information and user authentication during HTML-to-PDF conversions.

What is the difference between custom cookies and standard cookies in IronPDF?

Custom cookies in IronPDF handle complex authentication systems or pass application-level data with key-value pairs, while standard cookies manage basic HTTP authentication and session management.

How can I apply custom cookies using IronPDF?

To apply custom cookies, set the `CustomCookies` property with a dictionary of key-value pairs and use methods like `RenderUrlAsPdf` to generate PDFs with these settings.

Which `RequestContext` should I choose for maintaining session state in IronPDF?

Choose `Global` for shared contexts to maintain session data across operations, or `Isolated` to prevent interactions between renders.

What are some common issues when implementing cookies in PDF generation?

Common issues include expired cookies, domain mismatches, secure cookie configurations for HTTPS, and SameSite policy limitations affecting cross-origin requests.

How do I troubleshoot cookie expiration issues in IronPDF?

Verify cookies' validity before rendering and implement refresh logic for short-lived tokens to handle expiration issues during PDF generation.

How does IronPDF ensure security with secure cookies?

Properly configure secure cookies by ensuring they are only sent over HTTPS and match the required security policies when rendering PDFs.

Can IronPDF manage cookies for JavaScript-heavy sites?

Yes, IronPDF can manage cookies for JavaScript-heavy sites that rely on cookies for state management by using custom cookies or specific rendering options.

How can IronPDF be used with Blazor Server applications?

When implementing cookies in Blazor Server applications, choose the appropriate `RequestContext` to maintain proper session state between server-side renders.

What should I consider when using IronPDF's `ApplyCookies` method?

Ensure proper setup of the `RequestContext` and use valid HTTP credentials, verifying against your application's authentication requirements before applying cookies.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 20,756,830Version:2026.8just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.8

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.8

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required