跳過到頁腳內容
.NET幫助

IdentityServer .NET(對開發者如何理解的工作)

在當今的軟體架構中,安全性至關重要,尤其是涉及用戶權限和認證時。 As a top framework for implementing OpenID Connect and OAuth 2.0, IdentityServer4 offers a reliable option for centralized authorization and authentication in distributed networks. 當開發人員使用IronPDF這種強大的C# PDF生成庫和安全的身份管理時,他們可以輕鬆結合兩者來創建滿足各種應用程序需求的PDF文檔。

IdentityServer4提供了一種基於標準的模塊化解決方案,簡化了身份管理的安裝。 它幫助開發人員創建一個集中身份提供者,負責處理用戶認證、訪問、令牌驗證和簽發以及各種服務和應用的權限驗證。IdentityServer4 支持多種認證方法,使開發人員可以創建安全且直觀的認證體驗,例如用戶名/密碼、社交登錄和多因素認證。

This tutorial will cover the C# integration of IdentityServer4 with IronPDF, showing how to use IdentityServer4 to create secure processes for authorization and authentication and how to use the user identities created to customize IronPDF's PDF document creation. 我們將介紹如何改善C#應用程序的安全性和功能性,從將IdentityServer4配置為集中身份和認證提供者到集成IronPDF進行動態PDF生產。

IdentityServer4 C#是什麼?

名為IdentityServer4的受歡迎開源框架被用於.NET和C#應用程序中執行身份管理、授權和認證。 它是一種靈活的解決方案,符合現代安全協議,如OpenID Connect和OAuth 2.0,能夠保護網頁應用、API和微服務。

IdentityServer4基本上作為一個集中認證伺服器,處理用戶身份識別和權限管理的核心身份、有效訪問令牌簽發和憑證驗證。 它使程序員能夠將聯合認證和單一登錄(SSO)集成到多個應用程序及服務中,由此帶來安全且無縫的終端用戶體驗。

IdentityServer.NET(針對開發者的工作原理):圖1 - IdentityServer4: 使用ASP.NET Core Identity

IdentityServer4的特性

單一登錄(SSO)

用戶只需一次驗證即可訪問不同的應用或服務,而無需重新輸入其憑據。

支持OpenID Connect和OAuth 2.0

IdentityServer4 提供工業標準的安全認證和授權協議,具備廣泛的客戶應用和平台的兼容性和支持。

可調整配置

開發人員可以自定義安全設置以滿足特定應用程序要求,並擁有對認證和權限策略配置的細微控制。

連接到ASP.NET Core

得益於IdentityServer4 與框架的無縫集成,ASP.NET Core網頁應用和API的認證可以輕易實施。

個性化和適應性

由於該框架具有高度的擴展性和可定制性,開發人員可以根據需要增加新的用戶存儲、身份提供者、測試用戶和認證工作流程。

用戶驗證

IdentityServer4讓開發人員能夠選擇最適合其網頁應用要求的認證機制配置。 這些機制包括用戶名/密碼、社交登錄(如Google、Facebook等)及外部身份提供者(如Active Directory、Azure AD等)。

授權政策

程序員可以根據用戶角色、聲明或其他標準創建細顆粒度的授權政策,以確保只有授權用戶可訪問特定資源或在應用程序內執行特定操作。

令牌管理

IdentityServer4管理訪問令牌、令牌刷新和身份令牌,提供由身份伺服器保護資源的用戶認證和權限的安全方法。

創建和配置

在Visual Studio中的C#專案中設置IdentityServer4需按以下步驟進行:

在Visual Studio中創建一個新專案

啟動Visual Studio應用後,選擇“創建新專案”選項,或選擇文件菜單 > 添加 > “新專案”。 然後選擇“新的項目”之後,再選擇“ASP.NET Core Web App(模型-視圖-控制器)”。 本教程中將使用該應用程序進行PDF文檔生成。

 related to 在Visual Studio中創建一個新專案 選擇新專案選項。 然後選擇“ASP.NET Core Web App”" />

在相應文本框中選擇文件路徑並輸入專案名稱。 接下來,點擊創建按鈕來選擇所需的.NET Framework,如下圖所示。

IdentityServer.NET(針對開發者的工作原理):圖3 - 接下來配置專案,指定項目名稱和位置。 點擊下一步。

決定需要的框架並點擊創建按鈕。

IdentityServer.NET(針對開發者的工作原理):圖4 - 指定額外信息,如框架、驗證類型,選擇是否配置為HTTPs和啟用Docker。 然後點擊創建。

Visual Studio專案將生成所選應用程序的結構。 在本實例中,我們使用ASP.NET MVC。 要編寫代碼,我們可以創建新的控制器或利用現有控制器來輸入代碼並構建/運行程式。

IdentityServer.NET(針對開發者的工作原理):圖5 - ASP.NET Core Web App (MVC)專案成功創建。

要測試代碼,添加必要的庫。

安裝IdentityServer4套件

使用.NET CLI或Visual Studio中的NuGet包管理器,將IdentityServer4包添加到專案中。 使用包管理器控制台或終端,輸入以下命令以安裝最新版本的套件:

Install-Package IdentityServer4

在.NET Core專案中配置IdentityServer4

在您的ASP.NET Core應用中,通過在Startup.cs文件中添加所需的身份認證中介件和服務來配置IdentityServer4。這裡提供了配置IdentityServer4的完整代碼示例:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
            .AddInMemoryClients(Config.Clients)
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryApiResources(Config.ApiResources)
            .AddTestUsers(Config.Users);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseIdentityServer();
    }
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
            .AddInMemoryClients(Config.Clients)
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryApiResources(Config.ApiResources)
            .AddTestUsers(Config.Users);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseIdentityServer();
    }
}
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Hosting

Public Class Startup
	Public Sub ConfigureServices(ByVal services As IServiceCollection)
		services.AddIdentityServer().AddInMemoryClients(Config.Clients).AddInMemoryIdentityResources(Config.IdentityResources).AddInMemoryApiScopes(Config.ApiScopes).AddInMemoryApiResources(Config.ApiResources).AddTestUsers(Config.Users)
	End Sub

	Public Sub Configure(ByVal app As IApplicationBuilder, ByVal env As IWebHostEnvironment)
		If env.IsDevelopment() Then
			app.UseDeveloperExceptionPage()
		End If
		app.UseIdentityServer()
	End Sub
End Class
$vbLabelText   $csharpLabel

配置客戶端、身份資源和API資源

配置IdentityServer4的客戶端、身份資源(作用域)和API資源是必要的。 這些配置可以在獨立的類中定義,例如Config.cs

public class Config
{
    public static IEnumerable<Client> Clients { get; set; }
    public static IEnumerable<IdentityResource> IdentityResources { get; set; }
    public static IEnumerable<ApiScope> ApiScopes { get; set; }
    public static IEnumerable<ApiResource> ApiResources { get; set; }
    public static List<TestUser> Users { get; set; }
}
public class Config
{
    public static IEnumerable<Client> Clients { get; set; }
    public static IEnumerable<IdentityResource> IdentityResources { get; set; }
    public static IEnumerable<ApiScope> ApiScopes { get; set; }
    public static IEnumerable<ApiResource> ApiResources { get; set; }
    public static List<TestUser> Users { get; set; }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

通過遵循這些步驟,您可以在ASP.NET Core應用中通過在C#專案中創建和配置IdentityServer4來啟用安全的認證和授權。

使用IdentityServer4和IronPDF入門

These instructions will guide you through configuring IdentityServer4 for secure authentication and authorization and using IronPDF to create PDF documents in a C# project. 您將學習如何通過以下步驟基於基本設置創建專案。

什麼是 IronPDF?

IronPDF是一個功能豐富的庫,用於在.NET應用程序中與PDF文檔交互。 憑借其廣泛的功能集,用戶可以從頭開始或從HTML內容創建PDF,以及添加、移除或重新排列現有PDF文檔的部分。 IronPDF提供給開發人員一個強大的API,用於創作、修改和轉換PDF文件,簡化.NET應用程序中的PDF操作。

IdentityServer.NET(針對開發者的工作原理):圖6 - IronPDF for .NET: C# PDF庫

IronPDF的核心特性

HTML到PDF轉換

使用IronPDF,您可以生成高質量的PDF文檔,呈現HTML內容,包括JavaScript和CSS。 此功能在從網站或動態內容創建PDF時非常有用。

修改和增強PDF

IronPDF允許您更改已存在的PDF文檔。您可以將多個PDF合併為一個文檔,從PDF中提取頁面,並添加文本、圖像、水印或註釋。

動態創建PDF

使用IronPDF的API,您可以程式化地添加文本、圖像、形狀和其他對像到新創建的PDF文檔中。 這允許為發票、報告等基於文檔的輸出進行動態PDF生成。

PDF 安全性

You can manage access and safeguard sensitive data by encrypting PDF documents with IronPDF and adding password protection.

PDF文檔

IronPDF允許用戶創建、填寫及提交PDF表單, 並將數據填入表單字段。

文本提取

IronPDF幫助您通過從PDF文檔中提取文字信息來處理、分析和搜索文本數據。

轉換為圖像格式

IronPDF能將PDF文檔轉換為通用圖像格式,如PNG、JPEG和BMP,當需要圖像代替PDF時非常有用。

首先,確保你的項目安裝了 IronPDF 庫。

用.NET CLI或NuGet包管理器將最新版的IronPDF添加到您專案中。

Install-Package IronPdf

將IronPDF與IdentityServer4 C#整合

Startup.cs文件中設置所需的服務和中介件來進行IdentityServer4的初始化,如上面的代碼所示。 然後創建一個名為PdfController.cs的新MVC控制器,以使用IronPDF進行PDF生成。

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using IronPdf;
using System.Threading.Tasks;

[Authorize]
public class PdfController : Controller
{
    public async Task<IActionResult> GeneratePdf()
    {
        // Create IronPDF Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // HTML content to be converted to PDF
        string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a generated PDF document.</p>";

        // Convert HTML to PDF asynchronously
        var pdfDocument = await Task.Run(() => renderer.RenderHtmlAsPdf(htmlContent));

        // Return the PDF as a file
        return File(pdfDocument.BinaryData, "application/pdf", "example.pdf");
    }
}
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using IronPdf;
using System.Threading.Tasks;

[Authorize]
public class PdfController : Controller
{
    public async Task<IActionResult> GeneratePdf()
    {
        // Create IronPDF Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // HTML content to be converted to PDF
        string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a generated PDF document.</p>";

        // Convert HTML to PDF asynchronously
        var pdfDocument = await Task.Run(() => renderer.RenderHtmlAsPdf(htmlContent));

        // Return the PDF as a file
        return File(pdfDocument.BinaryData, "application/pdf", "example.pdf");
    }
}
Imports Microsoft.AspNetCore.Authorization
Imports Microsoft.AspNetCore.Mvc
Imports IronPdf
Imports System.Threading.Tasks

<Authorize>
Public Class PdfController
	Inherits Controller

	Public Async Function GeneratePdf() As Task(Of IActionResult)
		' Create IronPDF Renderer
		Dim renderer = New IronPdf.ChromePdfRenderer()

		' HTML content to be converted to PDF
		Dim htmlContent As String = "<h1>Hello, IronPDF!</h1><p>This is a generated PDF document.</p>"

		' Convert HTML to PDF asynchronously
		Dim pdfDocument = Await Task.Run(Function() renderer.RenderHtmlAsPdf(htmlContent))

		' Return the PDF as a file
		Return File(pdfDocument.BinaryData, "application/pdf", "example.pdf")
	End Function
End Class
$vbLabelText   $csharpLabel

將IronPDF與IdentityServer4集成涉及到首先設置IdentityServer4進行安全的用戶驗證和授權,然後使用IronPDF創建僅授權用戶可訪問的PDF文檔。 在提供的代碼示例中,Startup.cs文件中配置了IdentityServer4,通過記憶體內配置和客戶端憑證處理用戶身份管理。

PdfController[Authorize]屬性保護,確保只有授權用戶能夠訪問其操作。 該控制器使用異步方法渲染HTML內容到PDF格式,使用IronPDF庫。 這種PDF生成過程涉及創建一個ChromePdfRenderer渲染器,將HTML內容轉換為PDF文檔,並將PDF作為文件響應返回。

通過在受保護的端點內嵌入PDF生成邏輯,只有經IdentityServer4認證的用戶才能啟動PDF生成,從而結合強大的安全性和動態文件生成能力。 此設置對於需要安全文件處理的應用程序尤其有優勢,例如生成發票、報告或者基於用戶特定信息的個性化內容,同時通過IdentityServer4強制執行嚴格的訪問控制。

IdentityServer.NET(針對開發者的工作原理):圖7 - 使用IronPDF生成的輸出PDF,經IdentityServer4提供的安全用戶認證和授權。

結論

In summary, the integration of IdentityServer4 with IronPDF in a C# project effectively merges robust security with dynamic PDF generation capabilities. IdentityServer4提供了一種統一和標準化的方法來管理多個應用和服務的用戶身份和訪問控制,確保安全的用戶認證和授權。 使用IronPDF,開發人員能夠生成僅授權用戶可訪問的高質量PDF文檔,皆基於經驗證的用戶數據。

這種整合增強了應用程序的安全性和功能,使其在生成需要安全文檔處理的情景中非常理想,如生成發票、報告及個性化內容。 Overall, the combination of IdentityServer4 and IronPDF provides a compelling solution for developing secure, efficient, and user-oriented applications within the .NET framework.

When IronPDF and Iron Software technologies are integrated into your enterprise application development stack, IronPDF can provide feature-rich developer documentation and premium software solutions for customers and end users. 這一扎實基礎還將促進專案、后端系統及過程的改進。

IronPDF Suite, a combination of 9 different .NET API products, is available for a 競爭性的許可價格供貨! 這些技術由於其全面的文檔、活躍的在線開發者社區和定期更新而成為現代軟體開發項目中的絕佳選擇。

常見問題解答

IronPDF 如何在 .NET 應用程式中轉換 HTML 為 PDF?

IronPDF 提供了像 RenderHtmlAsPdf 這樣的方法,允許開發人員在 .NET 應用程式中將 HTML 字串轉換為 PDF 文件。它還支持將整個 HTML 文件或 URL 轉換為 PDF,使其成為文件轉換的一個多功能工具。

IdentityServer4 在 .NET 應用程式的安全中扮演什麼角色?

IdentityServer4 通過提供集中式的身份驗證和授權服務,在 .NET 應用程式的安全中發揮重要作用。它使開發人員能夠實施 OpenID Connect 和 OAuth 2.0 協議,允許安全的用戶管理和資源保護。

是否可以在 C# 應用程式中集成 PDF 生成與安全身份驗證?

可以,能夠在 C# 應用程式中集成 PDF 生成與安全身份驗證。通過使用 IdentityServer4 進行安全身份驗證和 IronPDF 生成 PDF,開發人員可以確保只有經身份驗證的用戶能夠訪問敏感文件,增強安全性和合規性。

在 .NET 專案中配置 IdentityServer4 的步驟是什麼?

要在 .NET 專案中配置 IdentityServer4,開發人員需要通過 NuGet 安裝 IdentityServer4 套件,在 Startup.cs 文件中進行配置,並設置認證用戶端。這確保應用程式可以安全地管理用戶身份和權限。

IronPDF 如何在 .NET 應用程式中增強文檔安全性?

IronPDF 通過允許開發人員向 PDF 文件添加加密和密碼保護等功能來增強文檔安全性。這確保 PDF 中的敏感資訊只可由授權用戶訪問,符合數據保護的最佳實踐。

IdentityServer4 支持的常見身份驗證方法有哪些?

IdentityServer4 支持常見的身份驗證方法,如用戶名/密碼,社交登錄和多因素身份驗證。這種靈活性允許開發人員創建安全且符合應用程式需求的用戶身份驗證流程。

如何管理 IdentityServer4 的令牌?

通過支持 OAuth 2.0,IdentityServer4 允許開發人員有效管理令牌。它提供了發行、驗證和撤銷令牌的功能,這對於根據用戶身份驗證狀態控制應用程式資源的訪問是必不可少的。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。