了解 IronPDF 中的 Kerberos

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

如何讓 UrlToPdf 與 Kerberos 驗證搭配使用?

若要將 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}");
            }
        }
    }
}
$vbLabelText   $csharpLabel

要點:

  • HttpClient 和 HttpClientHandler :使用UseDefaultCredentials = true HttpClientHandler來允許使用目前使用者的憑證進行 Kerberos 驗證。 -錯誤處理:實作 try-catch 區塊來管理 HTTP 請求期間的異常。
  • HTML 渲染:取得 HTML 後,如有必要,可使用 IronPDF 將內容渲染成 PDF。
柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 17,012,929 | 版本: 2025.12 剛剛發布