Understanding Kerberos in IronPDF

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

如何使用 Kerberos 认证使 UrlToPdf 工作?

要使用 Kerberos 认证与 IronPDF 的 UrlToPdf,您应该在渲染设置中设置用户名和密码。 您可以参考 IronPDF 文档获取更多详情:IronPdf.ChromeHttpLoginCredentials

我们推荐使用 System.Net.Http.HttpClient 下载 HTML 内容,随后您可以进行渲染。 这种方法允许您在 IronPDF 处理内容之前处理包括需要认证的 HTTP 请求。

这里有一个关于使用 Kerberos 下载页面的在线指南:System.Net.Http.HttpClient 如何选择认证类型?。 这个 StackOverflow 链接提供了有关使用 HttpClient 实现认证的详细讨论。

为了解析并确保下载 HTML 中的所有必要资源,考虑使用 HTML Agility Pack。 这个 .NET 库有助于有效地操作和查询 HTML 文档。

// Example: Using HttpClient with Kerberos Authentication

// Import the necessary namespaces
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace KerberosAuthenticationExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Create an instance of HttpClient
            HttpClientHandler handler = new HttpClientHandler
            {
                // Automatically use default network credentials
                UseDefaultCredentials = true // Enables Windows authentication (e.g., Kerberos)
            };

            using HttpClient httpClient = new HttpClient(handler);

            try
            {
                // Send a GET request to the desired URL
                HttpResponseMessage response = await httpClient.GetAsync("https://your-secure-url.com");

                // Ensure the request was successful
                response.EnsureSuccessStatusCode();

                // Read and display the response body
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);

                // If needed, render the HTML to PDF with IronPDF here
                // IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
                // renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
            }
            catch (HttpRequestException e)
            {
                // Handle any error responses from the server or connection issues
                Console.WriteLine("\nException Caught!");
                Console.WriteLine($"Message :{e.Message}");
            }
        }
    }
}
// Example: Using HttpClient with Kerberos Authentication

// Import the necessary namespaces
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace KerberosAuthenticationExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Create an instance of HttpClient
            HttpClientHandler handler = new HttpClientHandler
            {
                // Automatically use default network credentials
                UseDefaultCredentials = true // Enables Windows authentication (e.g., Kerberos)
            };

            using HttpClient httpClient = new HttpClient(handler);

            try
            {
                // Send a GET request to the desired URL
                HttpResponseMessage response = await httpClient.GetAsync("https://your-secure-url.com");

                // Ensure the request was successful
                response.EnsureSuccessStatusCode();

                // Read and display the response body
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);

                // If needed, render the HTML to PDF with IronPDF here
                // IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
                // renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
            }
            catch (HttpRequestException e)
            {
                // Handle any error responses from the server or connection issues
                Console.WriteLine("\nException Caught!");
                Console.WriteLine($"Message :{e.Message}");
            }
        }
    }
}
' Example: Using HttpClient with Kerberos Authentication

' Import the necessary namespaces
Imports Microsoft.VisualBasic
Imports System
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Threading.Tasks

Namespace KerberosAuthenticationExample
	Friend Class Program
		Shared Async Function Main(ByVal args() As String) As Task
			' Create an instance of HttpClient
			Dim handler As New HttpClientHandler With {.UseDefaultCredentials = True}

			Using httpClient As New HttpClient(handler)
	
				Try
					' Send a GET request to the desired URL
					Dim response As HttpResponseMessage = Await httpClient.GetAsync("https://your-secure-url.com")
	
					' Ensure the request was successful
					response.EnsureSuccessStatusCode()
	
					' Read and display the response body
					Dim responseBody As String = Await response.Content.ReadAsStringAsync()
					Console.WriteLine(responseBody)
	
					' If needed, render the HTML to PDF with IronPDF here
					' IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
					' renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
				Catch e As HttpRequestException
					' Handle any error responses from the server or connection issues
					Console.WriteLine(vbLf & "Exception Caught!")
					Console.WriteLine($"Message :{e.Message}")
				End Try
			End Using
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

关键点:

  • HttpClient 和 HttpClientHandler:使用带有 UseDefaultCredentials = trueHttpClientHandler 允许使用当前用户的凭证进行 Kerberos 认证。
  • 错误处理:实现 try-catch 块以管理 HTTP 请求期间的异常。
  • HTML 渲染:一旦 HTML 被获取,必要时利用 IronPDF 将内容渲染成 PDF。
Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布