> For the complete documentation index, see [llms.txt](https://help.aikido.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.aikido.dev/docs/docs-ja/zen-firewall/zen-installation-instructions/zen-firewall-for-dotnet/aspnet-core.md).

# ASP.NET Core

このガイドでは、Aikido の Zen Firewall をアプリケーションにインストールしてセットアップする手順を順を追ってご案内します。以下の手順に従って、アプリケーションを保護してください。

問題や不具合が発生した場合は、サポートチャットまたは GitHub Issues で遠慮なくご連絡ください。

<https://github.com/AikidoSec/firewall-dotnet>

## 必要条件

* C#/.NET 6+（.NET 10 までテスト済み）。
* ASP.NET Core 6.0 以上。
* [Aikidoアカウント](/docs/docs-ja/hajimeni/setting-up-your-account.md) & [Zen Firewallトークン](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md)

## インストールと設定

{% stepper %}
{% step %}
**AikidoのZen Firewallをインストール**

プロジェクトが .NET 6、7、8、9、または 10 で動作することを確認してください。さらに、アプリケーションではエンドポイント ルーティング（`UseRouting`）を使用する必要があります。これにより、Zen Firewall がルート情報を正しく解決できます。次のような従来のルーティング ミドルウェアは `UseMvc` サポートされていません。ASP.NET Core の移行ガイドを参照してください [こちら](https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30).

NuGet からパッケージをインストールします:

```bash
dotnet add package Aikido.Zen.DotNetCore
```

トークンを次で設定します `appsettings.json` （または設定します `AIKIDO_TOKEN` 環境変数として）：

```json
{
  "Aikido": {
    "AikidoToken": "your-api-key"
  }
}
```

スタートアップ構成で Zen を有効にします:

{% @aikido-custom-code/code-highlight language="csharp" content="! Program.cs
+using Aikido.Zen.DotNetCore;
+using Microsoft.AspNetCore.HttpOverrides;

public void ConfigureServices(IServiceCollection services)
{
// other services

* services.AddZenFirewall();

```
// other services
```

}

public void Configure(IApplicationBuilder app)
{
// Make sure to allow x-forwarded-for header if your app is behind a proxy

* app.UseForwardedHeaders(new ForwardedHeadersOptions
* {
* ```
     ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
  ```
* });

```
// other middleware
```

```
// app.UseRouting()
// place this after .UseRouting(), or after authorization,
// but high enough in the pipeline to catch all requests
```

* app.UseZenFirewall();

```
// other middleware like app.UseEndpoints() or app.MapControllers()
```

}
" %}
{% endstep %}

{% step %}
**ユーザー識別とブロックを有効にする**

このミドルウェアを使用して現在のユーザーを識別し、ユーザーブロックなどのユーザーベースの機能を有効にします。

Zen Firewall は、攻撃をブロックするためにこのミドルウェアを必要としません。基本的な攻撃保護はこれなしで動作します。Zen にどのユーザーがリクエストしているかを認識させたい場合に、このミドルウェアを追加してください。

アプリケーションがユーザーを識別する方法に合わせて、例を調整してください。

{% @aikido-custom-code/code-highlight language="csharp" content="! Program.cs

// authentification middleware

+app.Use((context, next) => {

* ```
  // unique id for the user
  ```
* ```
  var id = context.User?.Identity?.GetUserId();
  ```
* ```
  // name (optional)
  ```
* ```
  var name = context.User?.Identity?.Name;
  ```
* ```
  if (!string.IsNullOrEmpty(id))
  ```
* ```
      Zen.SetUser(id, name, context);
  ```
* ```
  return next();
  ```
* })

app.UseZenFirewall(); // place this after UseRouting

// other middleware like UseEndpoints() or MapControllers()" %}
{% endstep %}

{% step %}
**Zen Firewallをdry / 検出のみモードで起動する**

Aikido Zen エージェントが取得できるように、トークンを環境変数として設定してください。まだトークンがない場合は、次に従ってください [こちらの手順](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md).

```bash
AIKIDO_TOKEN=AIK_RUNTIME_
```

リクエストをブロックせずに期待どおりに動作することを確認するため、アプリをドライモードで起動することを推奨します。誤検知を避けるため、Zen Firewall はステージング環境で 2 週間実行することをお勧めします。

```bash
AIKIDO_BLOCK=false
```

{% hint style="info" %}
次を使用できます `AIKIDO_DEBUG=true` エージェントが何をしているかについてより詳細な情報を得るために、デバッグモードを有効にします。環境変数の詳細については、次を参照してください： [環境変数による設定](/docs/docs-ja/zen-firewall/zen-installation-instructions/configuration-via-environment-variables.md)
{% endhint %}
{% endstep %}

{% step %}
**アプリをテストする**

アプリケーションにアクセスして、いくつか操作を行うか、いくつかのページを開いてください。Zen がアプリケーション内のルートを自動的に検出します。

{% hint style="info" %}
Zen は 10 分ごとに Aikido にデータを送信します
{% endhint %}

以下の Zen アプリケーションのページを確認することで、エージェントが正常に動作しているか検証できます：

* **イベント**: 「Application started」イベントが表示されるはずです。
* **ルート**: しばらくすると、アプリケーションのルートがここに、メソッド、ルート、リクエストとともに表示され始めます。
* **インスタンス**: Zen がインストールされているアプリケーションのアクティブなインスタンス数が表示されるはずです。

<figure><img src="/files/bb6b16755cff4893513acdf0b399ab02ac7f5629" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**ダッシュボードでレート制限を設定する**

Zen Firewall ミドルウェアを追加したら、Aikido ダッシュボードでレート制限を設定することで、ルートをブルートフォース攻撃から保護するテストができます。

1. 作成したアプリをクリックします。
2. 次へ移動します： **ルート** タブ。
3. 制限したいルートを見つけてクリック **レート制限を設定**.
4. 指示に従ってレート制限を設定します（例: 1分あたり5リクエスト）。

![保護とレート制限のオプションがある認証ルートを表示する API ルート管理画面。](/files/e261faad47cc6b8291385535f551e58938a77367)

![POST /auth/login のレート制限を 1分あたり5リクエストに設定します。](/files/532f8ebb75a40ac740e8c3d35685a3aeb1b7e97d)

**レート制限を確認**

アプリを起動し、レート制限を設定したルートに1分以内に5回アクセスしてみてください。5回目の試行後、レート制限エラーが表示されるはずです:

```
Aikido ファイアウォールによりレート制限されています。（あなたの IP: 1.2.3.4）
```

{% endstep %}

{% step %}
**次のステップ**

Zen Firewall のインストールに成功しました。おめでとうございます。問題が発生した場合、懸念事項や機能要望がある場合は、遠慮なくサポートまでご連絡ください。

これで、Zen Firewall が提供する多くの機能を見て回ることができます:

* [Zen Firewall でボットトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-bot-traffic-with-zen-firewall.md)
* [Zen Firewall で Tor トラフィックをブロックまたは監視する](/docs/docs-ja/zen-firewall/zen-features/blocking-tor-traffic-with-zen-firewall.md)
* [Zen Firewall でユーザーを追跡する](/docs/docs-ja/zen-firewall/zen-features/blocking-users-with-zen-firewall.md)
* [Zen Firewall で既知の脅威アクターをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-known-threat-actors-with-zen-firewall.md)
* [Zen Firewall で国別にトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-traffic-by-country-with-zen-firewall.md)
* [ルートのレート制限を設定する](/docs/docs-ja/zen-firewall/zen-features/setting-up-rate-limiting-for-routes.md)
* [送信先ドメインを監視する](/docs/docs-ja/zen-firewall/zen-features/monitor-outbound-domains.md)

追加情報:

* [Zen のパフォーマンスと信頼性](/docs/docs-ja/zen-firewall/miscellaneous/how-zen-works-performance-reliability.md)
* [Zen Firewall のブロックモードと検出モード](/docs/docs-ja/zen-firewall/zen-features/blocking-vs-detection-mode-in-zen-firewall.md)
* [Zen 統計を理解する](/docs/docs-ja/zen-firewall/zen-features/understanding-your-zen-statistics.md)
  {% endstep %}
  {% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.aikido.dev/docs/docs-ja/zen-firewall/zen-installation-instructions/zen-firewall-for-dotnet/aspnet-core.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
