|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASP.NET Core 中的最少 API 允许我们构建具有最少依赖项的轻量级 API。然而,通常我们仍然需要在最小的 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 中内置的 Identity 框架提供了必要的中间件来实现身份验证、用户管理和基于角色的授权,使得在您的应用程序中更容易实现强大且安全的身份验证机制。
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-04-21 00:00:36
- 从模因币到海湖庄园,唐纳德·特朗普对加密货币的参与仍然是引人注目的余兴节目,在监管发展中引发了质疑。
-
-
- 比特币会议展望:“抛售新闻”是比特币价格游戏的名称吗?
- 2026-04-20 18:24:52
- 随着比特币会议的升温,交易员关注历史上的“抛售新闻”模式,比特币价格在投机泡沫和地缘政治暗流中表现出弹性。

































