.NET幫助 Dotnetopenauth .NET Core(對於開發者的運行原理) Curtis Chau 更新日期:6月 22, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article DotNetOpenAuth .NET Core 是原始 DotNetOpenAuth 庫的 .NET Core 版本,提供強大的公共 API。 此庫幫助您向 .NET 應用程式中添加 OAuth2 和 OpenID 驗證。 IronPDF 是一個在 .NET 中創建、閱讀和編輯 PDF 文件的庫。 它對於直接從 .NET 應用程式生成報告和發票等文檔非常有用。 您可以在各種類型的項目中使用 DotNetOpenAuth .NET Core 和 IronPDF,例如網頁和桌面應用程式,以利用共享代碼並實現新功能。 它們是開發人員處理身份驗證和 PDF 文檔管理的必備工具。 開始使用 DotNetOpenAuth .NET Core 在 .NET 項目中設置 DotNetOpenAuth .NET Core 要在受 Microsoft 技術支持的 .NET 項目中開始使用 DotNetOpenAuth .NET Core,請遵循以下步驟: 在 Visual Studio 中打開項目。 轉到方案資源管理器。 右鍵點擊您的項目名稱。 選擇 管理 NuGet 包。 在 NuGet 包管理器中,搜索 DotNetOpenAuth.NetCore 和其他 NuGet 包。 點擊 安裝 將其添加到您的項目中。 這將把 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 } Imports DotNetOpenAuth.OAuth2 ' Initialize the OAuth2 client with the authorization server details Private client = New WebServerClient(New AuthorizationServerDescription With { .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 Private state As IAuthorizationState = client.ProcessUserAuthorization() If state IsNot Nothing AndAlso state.IsAuthorized Then ' Authorized successfully, now you can access protected resources End If $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); Imports DotNetOpenAuth.OAuth2 ' Configure the OpenID Connect client with authority details Private openIdClient = New WebServerClient(New AuthorizationServerDescription With { .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 Private authUri As Uri = 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; } ' After the user is authenticated, process the authorization response to retrieve the token Dim authState As IAuthorizationState = openIdClient.ProcessUserAuthorization() If authState IsNot Nothing AndAlso authState.IsAuthorized Then ' Access token is available, and you can use it to make authenticated requests Dim accessToken As String = authState.AccessToken End If $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 } } ' Check if the access token is expired and refresh it If authState.AccessTokenExpirationUtc <= DateTime.UtcNow Then If openIdClient.RefreshAuthorization(authState) Then ' Token refreshed successfully End If End If $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 } ' Revoke the access token Dim success As Boolean = openIdClient.RevokeAuthorization(authState) If success Then ' Token revoked successfully End If $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 } ' Customize the token request with additional parameters Dim additionalParams = New Dictionary(Of String, String) From { {"custom_parameter", "value"} } Dim customizedState As IAuthorizationState = openIdClient.ProcessUserAuthorization(additionalParams) If customizedState IsNot Nothing AndAlso customizedState.IsAuthorized Then ' Token request customized and processed successfully End If $vbLabelText $csharpLabel 此代碼為令牌請求添加自定義參數,這對於應對授權伺服器的特定要求很有用。 DotNetOpenAuth .NET Core 與 IronPDF IronPDF 是一個全面的庫,使開發人員能夠在 .NET 環境中創建、閱讀和操作 PDF 文件。 特別適用於從 HTML 或直接從 URL 生成 PDF,非常適合報告、生成發票或僅以靜態格式存儲網頁。 與 DotNetOpenAuth .NET Core 集成時,確保這些功能僅對經身份驗證的用戶開放。 將 IronPDF 與 DotNetOpenAuth .NET Core 合併的使用情況 將 IronPDF 與 DotNetOpenAuth .NET Core 合併的一個實用案例是,已授權用戶需要在 Web 應用程式中生成和下載個性化報告。 例如,想象一下用戶登錄您的應用程式並以 PDF 格式訪問其財務報告的場景。 DotNetOpenAuth 確保用戶已經過正確身份驗證和授權訪問其文檔,而 IronPDF 負責創建和傳遞這些個性化 PDF。 使用案例代碼示例 讓我們看一下完整的代碼示例,展示如何實現這一點。 我們將在 .NET Core 中創建一個簡單的 Web API,這 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"); } } Imports IronPdf Imports Microsoft.AspNetCore.Mvc Imports Microsoft.AspNetCore.Authorization <Route("api/[controller]")> <ApiController> Public Class ReportController Inherits ControllerBase <Authorize> <HttpGet("download-pdf")> Public Function DownloadPdfReport() As IActionResult ' Authentication is handled by DotNetOpenAuth .NET Core Dim currentUser = HttpContext.User.Identity.Name ' Generate PDF content using IronPDF Dim Renderer = New ChromePdfRenderer() Dim 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 Dim 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") End Function End Class $vbLabelText $csharpLabel 在此示例中,我們使用 [Authorize] 屬性來確保只有經身份驗證的用戶才能訪問 PDF 生成端點。 IronPDF 的 ChromePdfRenderer 類用於從 HTML 內容創建 PDF,在此情況下,內容根據用戶姓名進行了動態個性化。 !DotNetOpenAuth .NET Core (它對開發人員的作用原理):圖 1 結論 將 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 以實現無縫驗證。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新日期 9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 更新日期 8月 5, 2025 C#開關模式匹配(對開發者來說是如何工作的) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 Specflow C#(對於開發者的運行原理)OpenTelemetry .NET(對於開發...