|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 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 の 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 には、ID 管理と呼ばれる強力な機能が含まれており、.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!」というテキストが表示されます。 Webブラウザに表示されます。このエンドポイントはこの記事の後半で使用します。
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) までご連絡ください。速やかに削除させていただきます。

































