市值: $2.5944T 1.80%
體積(24小時): $122.1521B -10.61%
  • 市值: $2.5944T 1.80%
  • 體積(24小時): $122.1521B -10.61%
  • 恐懼與貪婪指數:
  • 市值: $2.5944T 1.80%
加密
主題
加密植物
資訊
加密術
影片
頭號新聞
加密
主題
加密植物
資訊
加密術
影片
bitcoin
bitcoin

$87959.907984 USD

1.34%

ethereum
ethereum

$2920.497338 USD

3.04%

tether
tether

$0.999775 USD

0.00%

xrp
xrp

$2.237324 USD

8.12%

bnb
bnb

$860.243768 USD

0.90%

solana
solana

$138.089498 USD

5.43%

usd-coin
usd-coin

$0.999807 USD

0.01%

tron
tron

$0.272801 USD

-1.53%

dogecoin
dogecoin

$0.150904 USD

2.96%

cardano
cardano

$0.421635 USD

1.97%

hyperliquid
hyperliquid

$32.152445 USD

2.23%

bitcoin-cash
bitcoin-cash

$533.301069 USD

-1.94%

chainlink
chainlink

$12.953417 USD

2.68%

unus-sed-leo
unus-sed-leo

$9.535951 USD

0.73%

zcash
zcash

$521.483386 USD

-2.87%

加密貨幣新聞文章

在 ASP.NET Core 中以最少的 API 實作基於身分的驗證和授權

2024/05/23 17:03

ASP.NET Core 中的最少 API 允許我們建立具有最少依賴項的輕量級 API。然而,通常我們仍然需要在最小的 API 中進行身份驗證和授權。

在 ASP.NET Core 中以最少的 API 實作基於身分的驗證和授權

Minimal APIs in ASP.NET Core offer a lightweight approach to building APIs with minimal dependencies. However, many scenarios still require authentication and authorization in these minimal APIs. ASP.NET Core provides several options for achieving this, including basic authentication, token-based authentication, and identity-based authentication.

ASP.NET Core 中的最小 API 提供了一種輕量級方法來建立具有最小依賴性的 API。然而,許多場景仍然需要在這些最小的 API 中進行身份驗證和授權。 ASP.NET Core 提供了多種選項來實現此目的,包括基本驗證、基於令牌的身份驗證和基於身分的身份驗證。

We've covered implementing basic authentication in minimal APIs and JWT token-based authentication in minimal APIs. Now, let's explore how to implement identity-based authentication for minimal APIs in ASP.NET Core.

我們介紹了在最小 API 中實現基本身份驗證以及在最小 API 中實現基於 JWT 令牌的身份驗證。現在,讓我們探討如何在 ASP.NET Core 中為最少的 API 實作基於身分的驗證。

To follow along with the code examples in this article, ensure you have Visual Studio 2022 installed on your system. If you don't have a copy, you can download Visual Studio 2022 here.

若要按照本文中的程式碼範例進行操作,請確保您的系統上安裝了 Visual Studio 2022。如果您沒有副本,可以在此處下載 Visual Studio 2022。

Creating an ASP.NET Core Web API Project in Visual Studio 2022

在 Visual Studio 2022 中建立 ASP.NET Core Web API 項目

To create an ASP.NET Core Web API project in Visual Studio 2022, follow these steps:

若要在 Visual Studio 2022 中建立 ASP.NET Core Web API 項目,請依照下列步驟操作:

We'll use this ASP.NET Core Web API project to work with the code examples given in the sections below.

我們將使用此 ASP.NET Core Web API 專案來處理以下部分中給出的程式碼範例。

Identity Management in ASP.NET Core

ASP.NET Core 中的身分管理

ASP.NET Core includes a powerful feature called identity management, which has been enhanced in .NET 8. The built-in Identity framework in ASP.NET Core provides the necessary middleware to implement authentication, user management, and role-based authorization, making it easier to implement robust and secure authentication mechanisms in your application.

ASP.NET Core 包含一個名為身分識別管理的強大功能,在.NET 8 中得到了增強功能。的授權,使得在您的應用程式中更容易實現強大且安全的身份驗證機制。

ASP.NET Core's Identity framework is extensible and customizable, supporting the following key features:

ASP.NET Core 的 Identity 框架是可擴充且可自訂的,支援以下主要功能:

Creating a Minimal API in ASP.NET Core

在 ASP.NET Core 中建立最小 API

In the Web API project we created above, replace the generated code with the following code to create a basic minimal API.

在我們上面建立的 Web API 專案中,將產生的程式碼替換為以下程式碼以建立基本的最小 API。

When you run the application, the text "Hello World!" will be displayed in your web browser. We'll use this endpoint later in this article.

當您運行該應用程式時,會顯示文字“Hello World!”將顯示在您的網頁瀏覽器中。我們將在本文後面使用此端點。

Installing NuGet Packages

安裝 NuGet 套件

To add support for the built-in Identity framework in ASP.NET Core, select the project in the Solution Explorer window, then right-click and select "Manage NuGet Packages." In the NuGet Package Manager window, search for the Microsoft.AspNetCore.Identity.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer, and Microsoft.EntityFrameworkCore.Design packages and install them.

若要新增對 ASP.NET Core 內建 Identity 框架的支持,請在「解決方案資源管理器」視窗中選擇該項目,然後右鍵並選擇「管理 NuGet 套件」。在 NuGet 套件管理器視窗中,搜尋 Microsoft.AspNetCore.Identity.EntityFrameworkCore、Microsoft.EntityFrameworkCore.SqlServer 和 Microsoft.EntityFrameworkCore.Design 套件並安裝它們。

Alternatively, you can install the packages via the NuGet Package Manager console by entering the commands listed below.

或者,您可以透過 NuGet 套件管理器控制台輸入下面列出的命令來安裝套件。

Creating a New DbContext in EF Core

在 EF Core 中建立新的 DbContext

We'll be using Entity Framework Core in this example. The DbContext is an integral component of EF Core that represents a connection session with the database. Next, create a custom DbContext class by extending the IdentityDbContext class as shown in the code snippet given below.

我們將在此範例中使用 Entity Framework Core。 DbContext 是 EF Core 的一個組成部分,表示與資料庫的連線會話。接下來,透過擴充 IdentityDbContext 類別來建立自訂 DbContext 類,如下面給出的程式碼片段所示。

You should register the custom DbContext class by including the following line of code in the Program.cs file.

您應該透過在 Program.cs 檔案中包含以下程式碼行來註冊自訂 DbContext 類別。

Enabling Authentication and Authorization in ASP.NET Core

在 ASP.NET Core 中啟用身份驗證和授權

Authentication is the process of determining who the user is and validating the user's identity. You can enable authentication in a minimal API in ASP.NET Core by using the AddAuthentication() method as shown in the code snippet given below.

身份驗證是確定使用者是誰並驗證使用者身份的過程。您可以使用 AddAuthentication() 方法在 ASP.NET Core 中的最小 API 中啟用身份驗證,如下面給出的程式碼片段所示。

We use authorization to restrict access to certain resources in an application. You can enable authorization in your minimal API by using the following code.

我們使用授權來限制對應用程式中某些資源的存取。您可以使用以下程式碼在最小 API 中啟用授權。

The AddAuthorization method is used to register authorization services with the services container so that you can define rules for enabling or disabling access to resources of the application if needed.

AddAuthorization 方法用於向服務容器註冊授權服務,以便您可以根據需要定義啟用或停用對應用程式資源的存取的規則。

Configuring Services and API Endpoints in ASP.NET Core

在 ASP.NET Core 中設定服務和 API 端點

The next thing we need to do is configure the identity and EF Core services and the API endpoints. To do this, include the code listing given below in the Program.cs file.

我們需要做的下一件事是設定身分和 EF Core 服務以及 API 端點。為此,請將下面給出的程式碼清單包含在 Program.cs 檔案中。

The AddIdentityApiEndpoints() method in the preceding code snippet adds the necessary controllers and services for authentication and authorization (login, logout, registration, etc.). Note that this is a new method (introduced in .NET 8) used to configure Identity integration in an application. The AddIdentityApiEndpoints() method accepts an instance of type IdentityUser as a parameter, which is used to specify the type of user.

前面的程式碼片段中的 AddIdentityApiEndpoints() 方法新增了用於身份驗證和授權(登入、登出、註冊等)所需的控制器和服務。請注意,這是一種新方法(在 .NET 8 中引入),用於在應用程式中配置身份整合。 AddIdentityApiEndpoints() 方法接受 IdentityUser 類型的實例作為參數,該參數用於指定使用者的類型。

You can use the following piece of code to add authorization for the /helloworld endpoint.

您可以使用以下程式碼為 /helloworld 端點新增授權。

Complete Program.cs File Source

完整的 Program.cs 文件來源

The complete source code of the Program.cs file is given below for your reference.

以下給出Program.cs檔的完整原始碼,供大家參考。

The integrated identity management feature in ASP.NET Core is both powerful and easy to use. The improvements in .NET 8 have made Identity even more robust and flexible with an improved Identity API, which enables you to implement identity-based authentication and authorization more easily and efficiently with less code.

ASP.NET Core 中的整合式身分管理功能既強大又易於使用。 .NET 8 中的改進透過改進的 Identity API 使 Identity 變得更加強大和靈活,使您能夠使用更少的程式碼更輕鬆、更有效率地實現基於身分的身份驗證和授權。

Next read this:

接下來閱讀此內容:

免責聲明:info@kdj.com

所提供的資訊並非交易建議。 kDJ.com對任何基於本文提供的資訊進行的投資不承擔任何責任。加密貨幣波動性較大,建議您充分研究後謹慎投資!

如果您認為本網站使用的內容侵犯了您的版權,請立即聯絡我們(info@kdj.com),我們將及時刪除。

2026年05月02日 其他文章發表於