IronPDF 如何使用 TLS 網站與系統登入 在 C# 中,透過登入驗證將 HTML 轉換為 PDF Curtis Chau 更新:8月 14, 2025 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 處理登入的最佳方法是盡可能避免登錄,直接從文件或字串渲染 HTML。 快速入門:使用 IronPDF 在登入後將 HTML 轉換為 PDF 使用 IronPDF 簡單且有效率的 API,即使 HTML 頁面位於登入表單之後,也能輕鬆轉換為 PDF。 本快速入門指南向您展示如何使用 ChromeHttpLoginCredentials 方法進行驗證並擷取受保護的內容,從而確保無縫轉換過程。 無論您是處理網頁驗證還是 HTML 表單登錄,IronPDF 都能簡化流程,為您節省時間和精力。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronPDF PM > Install-Package IronPdf 複製並運行這段程式碼。 new ChromePdfRenderer { LoginCredentials = new ChromeHttpLoginCredentials("username","password") } .RenderUrlAsPdf("https://example.com/protected") .SaveAs("secure.pdf"); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronPDF,免費試用! 免費試用30天 最小工作流程(5 個步驟) 下載 C# IronPDF 庫 下載 HTML 以避免登入 使用LoginCredentials屬性透過網路驗證登入 使用 HTML 表單進行驗證 MVC 登入驗證的解決方法 最佳實務 IronPDF 支援 TLS 網路驗證(使用者名稱和密碼),.NET Web 應用程式可以輕鬆支援它: ChromeHttpLoginCredentials API 最佳實務是使用System.Net.WebClient或HttpClient下載 HTML 和所有資源。 它完全支援頁首、登入資訊以及您可能需要的所有其他功能。下載到記憶體或磁碟後,IronPDF 可以將您的 HTML 檔案轉換為 PDF 檔案。 可以使用HtmlAgilityPack來發現樣式表和圖像等資源,然後使用System.Net.WebClient下載這些資源。 // Download HTML content from a URL string html; using (WebClient client = new WebClient()) { html = client.DownloadString("http://www.google.com"); } // Load the HTML into an HtmlDocument HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); // Iterate through all image nodes and print their src attributes foreach(HtmlNode img in doc.DocumentNode.SelectNodes("//img")) { Console.WriteLine(img.GetAttributeValue("src", null)); } // Download HTML content from a URL string html; using (WebClient client = new WebClient()) { html = client.DownloadString("http://www.google.com"); } // Load the HTML into an HtmlDocument HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); // Iterate through all image nodes and print their src attributes foreach(HtmlNode img in doc.DocumentNode.SelectNodes("//img")) { Console.WriteLine(img.GetAttributeValue("src", null)); } ' Download HTML content from a URL Dim html As String Using client As New WebClient() html = client.DownloadString("http://www.google.com") End Using ' Load the HTML into an HtmlDocument Dim doc As New HtmlDocument() doc.LoadHtml(html) ' Iterate through all image nodes and print their src attributes For Each img As HtmlNode In doc.DocumentNode.SelectNodes("//img") Console.WriteLine(img.GetAttributeValue("src", Nothing)) Next img $vbLabelText $csharpLabel [{i:(任何相對 URL 都可以使用System.Uri類別的重載建構子重新定位為絕對 URL。 若要重新設定整個 HTML 文件中所有相對路徑的基準,請新增一個<base>使用 HtmlAgilityPack 將標籤新增至頭部。 範例.@@--括號結束--@@ 使用網路身份驗證登入 大多數 ASP.NET 應用程式支援網頁驗證,這比 HTML 表單提交更可靠。 :path=/static-assets/pdf/content-code-examples/how-to/logins-username-password.cs using IronPdf; using System; ChromePdfRenderer renderer = new ChromePdfRenderer { // setting login credentials to bypass basic authentication LoginCredentials = new ChromeHttpLoginCredentials() { NetworkUsername = "testUser", NetworkPassword = "testPassword" } }; var uri = new Uri("http://localhost:51169/Invoice"); // Render web URL to PDF PdfDocument pdf = renderer.RenderUrlAsPdf(uri); // Export PDF pdf.SaveAs("UrlToPdfExample.Pdf"); Imports IronPdf Imports System Private renderer As New ChromePdfRenderer With { .LoginCredentials = New ChromeHttpLoginCredentials() With { .NetworkUsername = "testUser", .NetworkPassword = "testPassword" } } Private uri = New Uri("http://localhost:51169/Invoice") ' Render web URL to PDF Private pdf As PdfDocument = renderer.RenderUrlAsPdf(uri) ' Export PDF pdf.SaveAs("UrlToPdfExample.Pdf") $vbLabelText $csharpLabel 使用 HTML 表單登入 也可以像前面的範例一樣,使用ChromeHttpLoginCredentials類,透過傳送資料到 HTML 表單來實現登入。 請參閱 IronPDF 的ChromeHttpLoginCredentials API 。 請考慮以下事項: 登入資料必須傳送到 HTML 表單 ACTION 屬性中指定的 URL。 這應該設定為 HttpLoginCredentials 的 \ LoginFormUrl \屬性。 這可能與您實際想要渲染為 PDF 的 URL 有所不同。 要傳送的資料應涵蓋 HTML 表單中的每個輸入框和文字區域。 name 屬性定義了每個變數的名稱(而不是 id,這一點常被誤解)。 有些網站可能會主動防範這類機器登入。 MVC 以下解決方法允許以程式設計方式將 .NET MVC 視圖呈現為字串,這對於避免 MVC 登入並忠實地呈現視圖非常有用。 // Converts an MVC partial view to a string public static string RenderPartialViewToString(this Controller controller, string viewPath, object model = null) { try { // Set the model var context = controller.ControllerContext; controller.ViewData.Model = model; using (var sw = new StringWriter()) { // Find the partial view var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath); if (viewResult.View == null) { throw new Exception($"Partial view {viewPath} could not be found."); } // Create a view context var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw); // Render the view viewResult.View.Render(viewContext, sw); viewResult.ViewEngine.ReleaseView(context, viewResult.View); return sw.GetStringBuilder().ToString(); } } catch (Exception ex) { // Return error message if there is an exception return ex.Message; } } // Converts an MVC partial view to a string public static string RenderPartialViewToString(this Controller controller, string viewPath, object model = null) { try { // Set the model var context = controller.ControllerContext; controller.ViewData.Model = model; using (var sw = new StringWriter()) { // Find the partial view var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath); if (viewResult.View == null) { throw new Exception($"Partial view {viewPath} could not be found."); } // Create a view context var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw); // Render the view viewResult.View.Render(viewContext, sw); viewResult.ViewEngine.ReleaseView(context, viewResult.View); return sw.GetStringBuilder().ToString(); } } catch (Exception ex) { // Return error message if there is an exception return ex.Message; } } ' Converts an MVC partial view to a string <System.Runtime.CompilerServices.Extension> _ Public Function RenderPartialViewToString(ByVal controller As Controller, ByVal viewPath As String, Optional ByVal model As Object = Nothing) As String Try ' Set the model Dim context = controller.ControllerContext controller.ViewData.Model = model Using sw = New StringWriter() ' Find the partial view Dim viewResult = ViewEngines.Engines.FindPartialView(context, viewPath) If viewResult.View Is Nothing Then Throw New Exception($"Partial view {viewPath} could not be found.") End If ' Create a view context Dim viewContext As New ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw) ' Render the view viewResult.View.Render(viewContext, sw) viewResult.ViewEngine.ReleaseView(context, viewResult.View) Return sw.GetStringBuilder().ToString() End Using Catch ex As Exception ' Return error message if there is an exception Return ex.Message End Try End Function $vbLabelText $csharpLabel 準備好看看您還能做些什麼嗎? 請造訪我們的教學頁面:轉換 PDF 文件 常見問題解答 如何在不需要登入認證的情況下將 HTML 轉換為 PDF? 若要在沒有登入驗證的情況下將 HTML 轉換為 PDF,請使用 IronPDF 直接從檔案或字串渲染 HTML。此方法可完全繞過認證需求。 在 C# 中將 HTML 轉換為 PDF 的初始步驟是什麼? 首先從 NuGet 下載 C# IronPDF Library。安裝完成後,您可以使用其方法直接將 HTML 文件渲染為 PDF,甚至可以處理具有登入驗證的情境。 建議使用哪些工具來安全地下載 HTML 內容,以便進行 PDF 轉換? 使用 System.Net.WebClient 或 HttpClient 下載 HTML 內容以及樣式表和圖片等資產。這些工具支援標頭和登入。HtmlAgilityPack 可用於管理資產發現,並確保下載所有必要的資源。 IronPDF 如何處理將 HTML 轉換為 PDF 的網路認證? IronPDF 支援 LoginCredentials 屬性的 TLS 網路驗證,提供 ASP.NET 應用程式處理驗證的安全方法。 是否可以使用 IronPDF 執行 HTML 表單驗證? 是的,您可以使用 IronPdf 的 ChromeHttpLoginCredentials 類來處理 HTML 表單驗證。請確保登入資料已提交至表單的 ACTION 屬性中指定的正確 URL。 轉換檢視為 PDF 時,如何繞過 MVC 登入驗證? 您可以程式化地將 MVC 視圖渲染為字串,這可以讓您繞過 MVC 登入,同時確保視圖的精確渲染。 使用 HTML 表單認證進行 PDF 轉換時,應採取哪些預防措施? 確保所有表單輸入和文字區域都能使用其名稱屬性在所張貼的資料中正確呈現。請注意某些網站可能有防止機器登入的保護措施。 IronPDF for .NET 使用登錄憑證將受保護的 URL 轉換為 PDF 時,是否與 .NET 10 完全相容? 是 - IronPDF 支持与 .NET 10 的完全兼容,包括在将受保护的 URL 呈现为 PDF 时使用 ChromeHttpLoginCredentials 进行基于网络和表单的身份验证。([ironpdf.com](https://ironpdf.com/blog/net-help/net-10-features/?utm_source=openai)) Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 16,493,056 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:16,493,056 檢視授權