.NET幫助 Dotnetopenauth .NET Core(對於開發者的運行原理) Jacob Mellor 更新:2025年6月22日 下載 IronPDF NuGet 下載 DLL 下載 Windows Installer 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 DotNetOpenAuth .NET Core是原始DotNetOpenAuth程式庫的.NET Core版本,提供強大的公共API。 此程式庫幫助您將OAuth2和OpenID驗證添加到您的.NET應用程式中。 IronPDF是一個在.NET中創建、閱讀和編輯PDF檔案的程式庫。 它對於直接從您的.NET應用程式生成報告和發票等文件非常有用。 您可以在各種專案中使用DotNetOpenAuth .NET Core和IronPDF,如網頁和桌面應用程式,以利用共享代碼並實現新功能。 這些對於希望在其軟體中處理驗證和PDF文件管理的開發人員至關重要。 開始使用DotNetOpenAuth .NET Core 在.NET專案中設置DotNetOpenAuth .NET Core 要在您的.NET專案中開始使用DotNetOpenAuth .NET Core,透過微軟技術支持,請按以下步驟進行: 在Visual Studio中打開您的專案。 轉到解決方案總管。 右鍵點擊您的專案名稱。 選擇Manage NuGet Packages。 在NuGet Package Manager中,搜尋DotNetOpenAuth.NetCore和其他NuGet套件。 點擊Install將其添加到您的專案中。 這將把DotNetOpenAuth .NET Core程式庫添加到您的專案中,提供集成驗證功能的支持。 使用DotNetOpenAuth .NET Core的基本代碼示例 這是一個簡單的示例,展示如何使用DotNetOpenAuth .NET Core在您的應用程式中設置OAuth2驗證: using DotNetOpenAuth.OAuth2; // Initialize the OAuth2 client with the authorization server details var client = new WebServerClient(new AuthorizationServerDescription { TokenEndpoint = new Uri("https://your-auth-server.com/token"), AuthorizationEndpoint = new Uri("https://your-auth-server.com/authorize") }, "your-client-id", "your-client-secret"); // Start the authentication process and get the authorization state IAuthorizationState state = client.ProcessUserAuthorization(); if (state != null && state.IsAuthorized) { // Authorized successfully, now you can access protected resources } using DotNetOpenAuth.OAuth2; // Initialize the OAuth2 client with the authorization server details var client = new WebServerClient(new AuthorizationServerDescription { TokenEndpoint = new Uri("https://your-auth-server.com/token"), AuthorizationEndpoint = new Uri("https://your-auth-server.com/authorize") }, "your-client-id", "your-client-secret"); // Start the authentication process and get the authorization state IAuthorizationState state = client.ProcessUserAuthorization(); if (state != null && state.IsAuthorized) { // Authorized successfully, now you can access protected resources } $vbLabelText $csharpLabel 此代碼片段使用DotNetOpenAuth .NET Core設置OAuth2客戶端,連接到授權伺服器,並處理使用者授權。 實施DotNetOpenAuth .NET Core的功能 集成OpenID Connect 要使用DotNetOpenAuth .NET Core集成OpenID Connect,您可以遵循此基本方法: using DotNetOpenAuth.OAuth2; // Configure the OpenID Connect client with authority details var openIdClient = new WebServerClient(new AuthorizationServerDescription { TokenEndpoint = new Uri("https://your-openid-provider.com/token"), AuthorizationEndpoint = new Uri("https://your-openid-provider.com/authorize") }, "your-client-id"); // Redirect user for authentication Uri authUri = openIdClient.GetAuthorizationRequestUri("openid email profile"); Response.Redirect(authUri.AbsoluteUri); using DotNetOpenAuth.OAuth2; // Configure the OpenID Connect client with authority details var openIdClient = new WebServerClient(new AuthorizationServerDescription { TokenEndpoint = new Uri("https://your-openid-provider.com/token"), AuthorizationEndpoint = new Uri("https://your-openid-provider.com/authorize") }, "your-client-id"); // Redirect user for authentication Uri authUri = openIdClient.GetAuthorizationRequestUri("openid email profile"); Response.Redirect(authUri.AbsoluteUri); $vbLabelText $csharpLabel 此代碼配置一個OpenID Connect客戶端,並將用戶重定向到OpenID提供商的驗證頁面。 處理訪問令牌 以下是您可以使用DotNetOpenAuth .NET Core處理訪問令牌的方法: // After the user is authenticated, process the authorization response to retrieve the token IAuthorizationState authState = openIdClient.ProcessUserAuthorization(); if (authState != null && authState.IsAuthorized) { // Access token is available, and you can use it to make authenticated requests string accessToken = authState.AccessToken; } // After the user is authenticated, process the authorization response to retrieve the token IAuthorizationState authState = openIdClient.ProcessUserAuthorization(); if (authState != null && authState.IsAuthorized) { // Access token is available, and you can use it to make authenticated requests string accessToken = authState.AccessToken; } $vbLabelText $csharpLabel 此片段處理使用者授權響應以檢索和使用訪問令牌。 刷新令牌 要在令牌過期時刷新令牌,請使用以下代碼: // Check if the access token is expired and refresh it if (authState.AccessTokenExpirationUtc <= DateTime.UtcNow) { if (openIdClient.RefreshAuthorization(authState)) { // Token refreshed successfully } } // Check if the access token is expired and refresh it if (authState.AccessTokenExpirationUtc <= DateTime.UtcNow) { if (openIdClient.RefreshAuthorization(authState)) { // Token refreshed successfully } } $vbLabelText $csharpLabel 此代碼檢查當前令牌是否已過期並嘗試刷新它。 撤銷令牌 如果您需要撤銷令牌,請按如下所示實現: // Revoke the access token bool success = openIdClient.RevokeAuthorization(authState); if (success) { // Token revoked successfully } // Revoke the access token bool success = openIdClient.RevokeAuthorization(authState); if (success) { // Token revoked successfully } $vbLabelText $csharpLabel 此片段撤銷授權,有效地使訪問令牌失效。 自訂令牌請求 要針對特定需求定製令牌請求,例如添加額外參數: // Customize the token request with additional parameters var additionalParams = new Dictionary<string, string> { {"custom_parameter", "value"} }; IAuthorizationState customizedState = openIdClient.ProcessUserAuthorization(additionalParams); if (customizedState != null && customizedState.IsAuthorized) { // Token request customized and processed successfully } // Customize the token request with additional parameters var additionalParams = new Dictionary<string, string> { {"custom_parameter", "value"} }; IAuthorizationState customizedState = openIdClient.ProcessUserAuthorization(additionalParams); if (customizedState != null && customizedState.IsAuthorized) { // Token request customized and processed successfully } $vbLabelText $csharpLabel 此代碼向令牌請求添加自訂參數,這對於處理特定授權伺服器的需求非常有用。 DotNetOpenAuth .NET Core與IronPDF IronPDF是一個全面的程式庫,允許開發人員在.NET環境中創建、閱讀和操作PDF文件。 它特別適合從HTML或直接從URL生成PDF,這對於報告、發票生成或僅以靜態格式存儲網頁非常好。 當與DotNetOpenAuth .NET Core集成時,它確保這些功能僅對已驗證的用戶安全且可訪問。 將IronPDF與DotNetOpenAuth .NET Core合併的使用案例 IronPDF與DotNetOpenAuth .NET Core合併實際用例是在網頁應用中,已驗證用戶需要生成和下載個人化報告。 例如,假設使用者登入您的應用程式並以PDF格式存取他們的財務報告。 DotNetOpenAuth確保使用者獲得適當的驗證和授權來訪問他們的文件,而IronPDF負責創建和傳送這些個性化的PDF。 用例的代碼範例 接下來讓我們看看一個完整的代碼示例,展示如何實現這一點。 我們將在.NET Core中創建一個簡單的Web API,認證用戶並使用IronPDF生成PDF報告: using IronPdf; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; [Route("api/[controller]")] [ApiController] public class ReportController : ControllerBase { [Authorize] [HttpGet("download-pdf")] public IActionResult DownloadPdfReport() { // Authentication is handled by DotNetOpenAuth .NET Core var currentUser = HttpContext.User.Identity.Name; // Generate PDF content using IronPDF var Renderer = new ChromePdfRenderer(); var PDF = Renderer.RenderHtmlAsPdf($"<h1>Report for {currentUser}</h1><p>This is your personalized financial report.</p>"); // Set file name and content type for the PDF var outputFileName = $"Report-{currentUser}.pdf"; Response.Headers.Add("Content-Disposition", $"attachment; filename={outputFileName}"); Response.ContentType = "application/pdf"; // Return the generated PDF file return File(PDF.Stream.ToArray(), "application/pdf"); } } using IronPdf; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; [Route("api/[controller]")] [ApiController] public class ReportController : ControllerBase { [Authorize] [HttpGet("download-pdf")] public IActionResult DownloadPdfReport() { // Authentication is handled by DotNetOpenAuth .NET Core var currentUser = HttpContext.User.Identity.Name; // Generate PDF content using IronPDF var Renderer = new ChromePdfRenderer(); var PDF = Renderer.RenderHtmlAsPdf($"<h1>Report for {currentUser}</h1><p>This is your personalized financial report.</p>"); // Set file name and content type for the PDF var outputFileName = $"Report-{currentUser}.pdf"; Response.Headers.Add("Content-Disposition", $"attachment; filename={outputFileName}"); Response.ContentType = "application/pdf"; // Return the generated PDF file return File(PDF.Stream.ToArray(), "application/pdf"); } } $vbLabelText $csharpLabel 在這個示例中,我們使用[Authorize]屬性來確保只有已驗證的使用者可以訪問PDF生成端點。 IronPDF中的ChromePdfRenderer類用於從HTML內容中創建PDF,在這種情況下,是動態個性化的用戶姓名。 結論 將DotNetOpenAuth .NET Core與IronPDF集成提供了一個強大的解決方案來增強.NET應用程式的安全性和功能。 透過利用這些技術,您可以有效地保護敏感數據,並通過動態PDF生成提供個性化的用戶體驗。 IronPDF不僅多功能而且易於開發人員使用,提供了一種簡單的方法來在.NET應用中創建和管理PDF文件。 如果您考慮將IronPDF納入您的專案,建議探索IronPDF官方網站獲得免費試用及授權選項。 常見問題解答 DotNetOpenAuth .NET Core 的用途是什麼? DotNetOpenAuth .NET Core 用於將 OAuth2 和 OpenID 驗證功能整合到 .NET 應用程式中,從而在網路和桌面環境中實現安全的驗證過程。 如何將 OAuth2 驗證新增到我的 .NET 應用程式中? 要新增 OAuth2 驗證,請使用 DotNetOpenAuth .NET Core 初始化具有授權伺服器詳細資訊的 WebServerClient,然後引導用戶完成驗證過程並處理其授權回應。 我可以在具有驗證的 .NET 應用程式中創建 PDF 嗎? 可以,通過整合 DotNetOpenAuth .NET Core 進行驗證和使用 IronPDF 進行 PDF 生成功能,您可以創建安全的已驗證 PDF 文件,例如個性化報告和發票。 您如何管理 .NET 應用程式中的存取權杖? DotNetOpenAuth .NET Core 通過處理授權回應來管理存取權杖,允許您檢索和利用存取權杖以進行安全的已驗證要求。 如何在 .NET Core 中刷新存取權杖? 要在 .NET Core 刷新存取權杖,請檢查權杖是否過期,並使用 `RefreshAuthorization` 方法獲取新權杖,以確保持續的安全訪問。 整合 DotNetOpenAuth 和 PDF 生成功能的好處是什麼? 整合 DotNetOpenAuth 和 PDF 生成功能允許安全訪問機密文件,讓已驗證用戶生成和下載個性化的 PDF,例如報告和發票。 您如何在 .NET 應用程式中撤銷權杖? 通過在 DotNetOpenAuth .NET Core 中實施 `RevokeAuthorization` 方法來撤銷權杖,這將失效存取權杖,以防止進一步的未授權要求。 如何在 .NET 驗證中自訂權杖要求? 您可以通過在權杖處理程式碼中新增額外參數來自訂權杖要求,以滿足授權伺服器的特定需求。 在 .NET 專案中設定 OpenID Connect 需要哪些步驟? 要設定 OpenID Connect,請配置具有所需授權詳細資訊的 OpenID Connect 客戶端,並引導用戶通過 OpenID 提供程式進行驗證,整合 DotNetOpenAuth .NET Core 以實現無縫驗證。 Jacob Mellor 立即與工程團隊聊天 首席技術官 Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。 相關文章 更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新2025年12月20日 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 Specflow C#(對於開發者的運行原理)OpenTelemetry .NET(對於開發...
更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多