|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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는 이를 달성하기 위해 기본 인증, 토큰 기반 인증, ID 기반 인증 등 여러 가지 옵션을 제공합니다.
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에 대한 ID 기반 인증을 구현하는 방법을 살펴보겠습니다.
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 웹 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의 ID 관리
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에서 향상된 ID 관리라는 강력한 기능이 포함되어 있습니다. ASP.NET Core에 내장된 ID 프레임워크는 인증, 사용자 관리 및 역할 기반 권한 부여를 구현하는 데 필요한 미들웨어를 제공합니다. 애플리케이션에서 강력하고 안전한 인증 메커니즘을 구현하는 것이 더 쉽습니다.
ASP.NET Core's Identity framework is extensible and customizable, supporting the following key features:
ASP.NET Core의 ID 프레임워크는 확장 및 사용자 지정이 가능하며 다음과 같은 주요 기능을 지원합니다.
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.
다음으로 해야 할 일은 ID와 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() 메서드는 인증 및 권한 부여(로그인, 로그아웃, 등록 등)에 필요한 컨트롤러와 서비스를 추가합니다. 이는 애플리케이션에서 ID 통합을 구성하는 데 사용되는 새로운 방법(.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의 통합 ID 관리 기능은 강력하고 사용하기 쉽습니다. .NET 8의 개선으로 인해 향상된 Identity API를 통해 Identity가 더욱 강력하고 유연해졌습니다. 이를 통해 더 적은 코드로 ID 기반 인증 및 권한 부여를 보다 쉽고 효율적으로 구현할 수 있습니다.
Next read this:
다음으로 읽어보세요:
부인 성명:info@kdj.com
제공된 정보는 거래 조언이 아닙니다. kdj.com은 이 기사에 제공된 정보를 기반으로 이루어진 투자에 대해 어떠한 책임도 지지 않습니다. 암호화폐는 변동성이 매우 높으므로 철저한 조사 후 신중하게 투자하는 것이 좋습니다!
본 웹사이트에 사용된 내용이 귀하의 저작권을 침해한다고 판단되는 경우, 즉시 당사(info@kdj.com)로 연락주시면 즉시 삭제하도록 하겠습니다.

































