Understanding Kerberos in IronPDF

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

如何使UrlToPdf在Kerberos身份驗證中運作?

要使用IronPDF的UrlToPdf進行Kerberos身份驗證,您應該在渲染設定中設置用戶名和密碼。 您可以參考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:使用HttpClientHandler並設置UseDefaultCredentials = true來允許使用當前用戶的憑證進行Kerberos身份驗證。
  • 錯誤處理:實現try-catch塊以管理HTTP請求過程中的異常。
  • HTML渲染:一旦HTML被獲取,如果需要,利用IronPDF將內容渲染成PDF。
Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布