# ASP.NET Core


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

問題や不具合が発生した場合は、遠慮なくサポートチャットまたは Github issues でお問い合わせください。

[https://github.com/AikidoSec/firewall-dotnet](https://github.com/AikidoSec/firewall-dotnet)

## 要件

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

## インストールと設定

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


プロジェクトが .NET 6、7、8、9、または 10 で動作していることを確認してください。また、Zen Firewall がルート情報を正しく解決できるように、アプリケーションでエンドポイントルーティング（`UseRouting`）を使用する必要があります。`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を有効にします：

```code-highlight language="csharp"
! 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 に認識させたい場合に、このミドルウェアを追加してください。

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

```code-highlight language="csharp"
! 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をドライ／検出専用モードで起動する**


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

```bash
AIKIDO_TOKEN=AIK_RUNTIME_
```

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

```bash
AIKIDO_BLOCK=false
```

{% hint style="info" %}
`AIKIDO_DEBUG=true` を使用するとデバッグモードが有効になり、エージェントの動作に関するより詳細な情報を確認できます。環境変数の詳細については、こちらをご覧ください: [configuration-via-environment-variables.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-installation-instructions/configuration-via-environment-variables "mention")
{% endhint %}
{% endstep %}

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


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

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

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

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

<figure><img src="https://help.aikidogov.us/_assets/c2caef7203b6/Screenshot-2025-06-23-at-10.08.35.png" alt=""><figcaption></figcaption></figure>
{% endstep %}

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


Zen Firewall ミドルウェアを追加したら、ルートをブルートフォース攻撃から保護する機能をテストできます。これは、Aikido Dashboard でレート制限を設定することで行います：

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

![認証ルートと保護およびレート制限のオプションを表示している API ルート管理画面。](https://help.aikidogov.us/_assets/5c97a4dd85f0/Screenshot-2025-10-30-at-10.38.17.png)

![POST /auth/login のレート制限を 1 分あたり 5 リクエストに設定。](https://help.aikidogov.us/_assets/a8c11e989a3b/ucarecdn-34b1af76-facb-4b6d-9c87-155236236b8f.png)

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

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

```
You are rate limited by Aikido firewall. (Your IP: 1.2.3.4)
```
{% endstep %}

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


Zen Firewall のインストールが完了しました。問題が発生した場合や、ご不明な点、機能のご要望がある場合は、お気軽にサポートまでお問い合わせください。

Zen Firewall が提供するさまざまな機能をぜひご覧ください：

* [blocking-bot-traffic-with-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-bot-traffic-with-zen-firewall "mention")
* [blocking-tor-traffic-with-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-tor-traffic-with-zen-firewall "mention")
* [blocking-users-with-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-users-with-zen-firewall "mention")
* [blocking-known-threat-actors-with-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-known-threat-actors-with-zen-firewall "mention")
* [blocking-traffic-by-country-with-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-traffic-by-country-with-zen-firewall "mention")
* [setting-up-rate-limiting-for-routes.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/setting-up-rate-limiting-for-routes "mention")
* [monitor-outbound-domains.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/monitor-outbound-domains "mention")

その他の情報：

* [how-zen-works-performance-reliability.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/miscellaneous/how-zen-works-performance-reliability "mention")
* [blocking-vs-detection-mode-in-zen-firewall.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/blocking-vs-detection-mode-in-zen-firewall "mention")
* [understanding-your-zen-statistics.md](https://help.aikidogov.us/docs/docs-ja/zen-firewall/zen-features/understanding-your-zen-statistics "mention")
{% endstep %}
{% endstepper %}
