IRONSOFTWAREHOME
開發者更新

Dotnetopenauth .NET Core(對於開發者的運行原理)

Jacob Mellor,首席技術官 @ Team Iron
Jacob Mellor
Updated: 2026年4月23日

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,並由 Microsoft 技術支持,請按照以下步驟進行:

  1. 在 Visual Studio 中打開您的專案。
  2. 前往方案總管。
  3. 右鍵點選您的專案名稱。
  4. 選擇 Manage NuGet Packages。
  5. 在 NuGet 套件管理器中,搜尋 DotNetOpenAuth.NetCore 和其他 NuGet 套件。
  6. 點選 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
}

此程式碼片段設置了一個使用 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);

此程式碼配置了一個 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;
}

此程式碼片段處理使用者授權響應以檢索和使用存取令牌。

刷新令牌

要在令牌過期時刷新它們,請使用以下程式碼:

// Check if the access token is expired and refresh it
if (authState.AccessTokenExpirationUtc <= DateTime.UtcNow)
{
    if (openIdClient.RefreshAuthorization(authState))
    {
        // Token refreshed successfully
    }
}

此程式碼檢查當前令牌是否過期並嘗試刷新它。

撤銷令牌

如果您需要撤銷令牌,請按照下面的顯示進行實施:

// Revoke the access token
bool success = openIdClient.RevokeAuthorization(authState);
if (success)
{
    // Token revoked 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
}

此程式碼向令牌請求中新增了自訂參數,這對處理授權伺服器的特定需求很有用。

DotNetOpenAuth .NET Core with 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");
    }
}

在這個例子中,我們使用 [Authorize] 屬性來確保只有經過身份驗證的使用者可以存取 PDF 生成端點。 IronPDF 中的 ChromePdfRenderer 類用來生成 HTML 內容的 PDF,在這個例子中,使用者的姓名會以動態個性化的方式展現在內容中。

DotNetOpenAuth .NET Core(對開發者的工作原理):圖1

結論

將 DotNetOpenAuth .NET Core 與 IronPDF 整合可提供強大的解決方案,以增強您 .NET 應用程式的安全性和功能。 通過利用這些技術,您可以有效保護敏感資料,並通過動態PDF生成提供個性化的使用者體驗。

IronPDF 不僅多才多藝,而且對開發者友好,提供建立和管理 .NET 應用程式中的 PDF 檔案的簡單方法。 如果您正在考慮將 IronPDF 加入您的專案,建議探索 IronPDF 官方網站 以獲取免費試用和授權選項。

Jacob Mellor,首席技術官 @ Team Iron
首席技術官

Jacob Mellor是Iron Software的首席技術官,一位在C# PDF技術上開創先河的遠見工程師。作為Iron Software核心程式碼庫的原開發者,他從創立以來就一直在塑造公司的產品架構,與首席執行官Cameron Rimington一起將公司轉變為服務於NASA、特斯拉和全球政府公司的50多名人員的公司。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密。
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起