How to use Cookies with IronPDF in C#
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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
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");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download IronPDF from NuGet
- Prepare the HTML content to render with custom cookies
- Configure the
RequestContextproperty to enable the use of cookies - Use the
ApplyCookiesmethod to apply cookies - Use the
CustomCookiesproperty to implement custom cookies
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:
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-cookies.csusing 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 toIronPdf.Rendering.RequestContexts.GlobalifIronPdf.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.
How Do I Apply Custom Cookies?
What Are Custom Cookies in IronPDF?
Custom cookies 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:
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-custom-cookies.csusing 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
What Are Common Issues with Cookie Implementation?
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:
- Cookie Expiration: Verify cookies haven't expired. Implement refresh logic for short-lived tokens.
- Domain Restrictions: Ensure cookie domains match the rendered URL.
- Secure Cookies: Configure secure cookies properly when rendering HTTPS URLs.
- 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 do I apply cookies when rendering HTML to PDF?
To apply cookies with IronPDF, set the RequestContext property to RequestContexts.Global on the ChromePdfRenderer, then use the ApplyCookies method with ChromeHttpLoginCredentials. This ensures cookies are properly transmitted during the HTML-to-PDF conversion process.
What is the RequestContext property used for?
The RequestContext property in IronPDF determines how cookies are shared between rendering sessions. It works with HTTP request headers and authentication, making it critical for applications that need to maintain session state across multiple PDF generations.
Can I add custom cookies during PDF rendering?
Yes, IronPDF allows custom cookies through the CustomCookies property. Simply create a Dictionary with your cookie key-value pairs and assign it to the RenderingOptions.CustomCookies property of the ChromePdfRenderer.
How do I render a cookie-protected page to PDF?
Use IronPDF's ChromePdfRenderer with the RequestContext set to Global and add your cookies via CustomCookies property. Then call RenderUrlAsPdf() with your protected URL. The renderer will include the cookies in the request, allowing access to protected content.
What types of authentication can cookies handle in PDF rendering?
IronPDF's cookie integration supports various authentication scenarios including TLS website authentication, system logins, and session-based authentication. This is particularly useful when converting ASPX pages or ASP.NET MVC applications that require user authentication.
Why are cookies important when converting HTML to PDF?
Cookies maintain state during the HTML to PDF conversion process with IronPDF's Chrome rendering engine. They're essential for rendering pages that require authentication, preserving user-specific preferences, and maintaining session information throughout the conversion.






