次のような場合に使用: ASP.NET Core Web APIのエンドポイント(公開されたデータやサービスへのアクセス地点)をJWT Bearer トークン(認証用の電子認証証)の検証、スコープチェック(利用範囲の確認)、DPoP バインディング(トークンと利用者の紐付け)で保護する場合。 Auth0.AspNetCore.Authentication.Api を統合し、フロントエンド(ユーザー側のアプリケーション)やモバイルアプリから送信されたアクセストークンを受け取るステートレス(状態を保持しない)な REST API で利用できます。
Use when protecting ASP.NET Core Web API endpoints with JWT Bearer token validation, scope checks, or DPoP binding. Integrates Auth0.AspNetCore.Authentication.Api for stateless REST APIs receiving access tokens from frontends or mobile apps.
Auth0.AspNetCore.Authentication.Api を使用して、JWT アクセストークン検証で ASP.NET Core Web API のエンドポイントを保護します。
auth0-quickstart スキルを使用してくださいAuth0.AspNetCore.Authentication)を使用auth0-react、auth0-vue、auth0-angular を使用auth0-react-native を使用dotnet add package Auth0.AspNetCore.Authentication.Api
Auth0 に API(Application ではなく)を作成する必要があります。
ここで一度停止し、ユーザーに確認してください。
他の操作を行う前に、以下の質問をそのまま尋ね、回答を待ってください:
「Auth0 API リソースの作成方法を選んでください:
- 自動 — Auth0 CLI スクリプトを実行してリソースを作成し、正確な値を appsettings.json に自動で書き込みます。
- 手動 — Auth0 Dashboard(または
auth0 apis create)で API を自分で作成し、Domain と Audience を教えてください。どちらを希望しますか?(1 = 自動 / 2 = 手動)」
ユーザーが回答するまで、いかなるセットアップ手順にも進まないでください。手動をデフォルトにしないでください。
自動を選んだ場合: 完全な CLI スクリプトは セットアップガイド を参照してください。自動の場合は appsettings.json が自動生成されるため、下記のステップ 3 をスキップしてステップ 4 に直接進んでください。
手動を選んだ場合: User Secrets や環境変数のオプションを含む詳細な手順は セットアップガイド(手動セットアップのセクション)を参照してください。その後、下記のステップ 3 に進んでください。
手動での API 作成のクイックリファレンス:
# Auth0 CLI を使用する場合
auth0 apis create \
--name "My ASP.NET Core API" \
--identifier https://my-api.example.com
または Auth0 Dashboard → Applications → APIs から手動で作成してください。
{
"Auth0": {
"Domain": "your-tenant.auth0.com",
"Audience": "https://my-api.example.com"
}
}
重要: Domain に https:// を含めないでください。ライブラリが Authority URL を自動的に構築します。
var builder = WebApplication.CreateBuilder(args);
// Auth0 JWT 検証を登録
builder.Services.AddAuth0ApiAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.JwtBearerOptions = new JwtBearerOptions
{
Audience = builder.Configuration["Auth0:Audience"]
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
// ミドルウェアの順序が重要: 認証(Authentication)は認可(Authorization)より前に配置
app.UseAuthentication();
app.UseAuthorization();
// エンドポイントをここに追加(ステップ 5 参照)
app.MapGet("/api/public", () => Results.Ok(new { message = "Public" }));
app.Run();
Minimal API の場合:
// パブリックエンドポイント — 認証不要
app.MapGet("/api/public", () => Results.Ok(new { message = "Hello from a public endpoint!" }));
// 保護されたエンドポイント — 有効な JWT が必要
app.MapGet("/api/private", (HttpContext ctx) =>
{
var userId = ctx.User.FindFirst("sub")?.Value;
return Results.Ok(new { message = "Hello from a protected endpoint!", userId });
}).RequireAuthorization();
コントローラーベースの場合:
[ApiController]
[Route("api")]
public class MessagesController : ControllerBase
{
[HttpGet("public")]
public IActionResult Public() =>
Ok(new { message = "Hello from a public endpoint!" });
[Authorize]
[HttpGet("private")]
public IActionResult Private() =>
Ok(new { message = "Hello from a protected endpoint!", userId = User.FindFirst("sub")?.Value });
}
パブリックエンドポイントのテスト:
curl http://localhost:5000/api/public
保護されたエンドポイントのテスト(アクセストークンが必要):
curl http://localhost:5000/api/private \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
テスト用トークンは、Client Credentials フローまたは Auth0 Dashboard → APIs → Test タブから取得してください。
| 間違い | 修正方法 |
|---|---|
Domain に https:// が含まれている |
your-tenant.auth0.com の形式のみ使用(スキームプレフィックスは不要) |
| Audience が API Identifier と一致しない | Auth0 Dashboard で設定した API Identifier と完全に一致させること |
| Auth0 に API ではなく Application を作成した | Auth0 Dashboard → Applications → APIs で API リソースを作成すること |
| ミドルウェアの順序が間違っている | UseAuthentication() は UseAuthorization() より前に配置すること |
| アクセストークンの代わりに ID トークンを使用している | API 認証には ID トークンではなくアクセストークンを使用すること |
| ローカル環境で HTTPS 証明書エラーが発生する | dotnet dev-certs https --trust を実行すること |
スコープポリシーの定義と適用については、インテグレーションガイド を参照してください。
RFC 9449 に準拠した Proof-of-Possession トークンバインディングをビルトインでサポートしています。設定方法については インテグレーションガイド を参照してください。
auth0-quickstart — Auth0 の基本セットアップauth0-mfa — 多要素認証(MFA)の追加auth0-cli — ターミナルから Auth0 リソースを管理設定オプション:
options.Domain — Auth0 テナントドメイン(https:// プレフィックスなし)(必須)options.JwtBearerOptions.Audience — Auth0 API 設定の API Identifier(必須)options.JwtBearerOptions — Microsoft JWT Bearer オプションへの完全アクセスユーザークレーム:
ctx.User.FindFirst("sub")?.Value — ユーザー ID(サブジェクト)ctx.User.FindFirst("scope")?.Value — スペース区切りのスコープ一覧ctx.User.FindAll("scope") — すべてのスコープクレーム主なユースケース:
.RequireAuthorization()(ステップ 5 参照)[Authorize] 属性(ステップ 5 参照)Protect ASP.NET Core Web API endpoints with JWT access token validation using Auth0.AspNetCore.Authentication.Api.
auth0-quickstart skill firstauth0-react, auth0-vue, or auth0-angular for client-side authauth0-react-native for React Native/Expodotnet add package Auth0.AspNetCore.Authentication.Api
You need an API (not Application) in Auth0.
STOP — ask the user before proceeding.
Ask exactly this question and wait for their answer before doing anything else:
"How would you like to create the Auth0 API resource?
- Automated — I'll run Auth0 CLI scripts that create the resource and write the exact values to your appsettings.json automatically.
- Manual — You create the API yourself in the Auth0 Dashboard (or via
auth0 apis create) and provide me the Domain and Audience.Which do you prefer? (1 = Automated / 2 = Manual)"
Do NOT proceed to any setup steps until the user has answered. Do NOT default to manual.
If the user chose Automated, follow the Setup Guide for complete CLI scripts. The automated path writes appsettings.json for you — skip Step 3 below and proceed directly to Step 4.
If the user chose Manual, follow the Setup Guide (Manual Setup section) for full instructions including User Secrets and environment variable options. Then continue with Step 3 below.
Quick reference for manual API creation:
# Using Auth0 CLI
auth0 apis create \
--name "My ASP.NET Core API" \
--identifier https://my-api.example.com
Or create manually in Auth0 Dashboard → Applications → APIs
{
"Auth0": {
"Domain": "your-tenant.auth0.com",
"Audience": "https://my-api.example.com"
}
}
Important: Domain must NOT include https://. The library constructs the authority URL automatically.
var builder = WebApplication.CreateBuilder(args);
// Register Auth0 JWT validation
builder.Services.AddAuth0ApiAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.JwtBearerOptions = new JwtBearerOptions
{
Audience = builder.Configuration["Auth0:Audience"]
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
// Middleware order matters: authentication before authorization
app.UseAuthentication();
app.UseAuthorization();
// Add your endpoints here (see Step 5)
app.MapGet("/api/public", () => Results.Ok(new { message = "Public" }));
app.Run();
Minimal API:
// Public endpoint - no authentication
app.MapGet("/api/public", () => Results.Ok(new { message = "Hello from a public endpoint!" }));
// Protected endpoint - requires valid JWT
app.MapGet("/api/private", (HttpContext ctx) =>
{
var userId = ctx.User.FindFirst("sub")?.Value;
return Results.Ok(new { message = "Hello from a protected endpoint!", userId });
}).RequireAuthorization();
Controller-based:
[ApiController]
[Route("api")]
public class MessagesController : ControllerBase
{
[HttpGet("public")]
public IActionResult Public() =>
Ok(new { message = "Hello from a public endpoint!" });
[Authorize]
[HttpGet("private")]
public IActionResult Private() =>
Ok(new { message = "Hello from a protected endpoint!", userId = User.FindFirst("sub")?.Value });
}
Test public endpoint:
curl http://localhost:5000/api/public
Test protected endpoint (requires access token):
curl http://localhost:5000/api/private \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Get a test token via Client Credentials flow or Auth0 Dashboard → APIs → Test tab.
| Mistake | Fix |
|---|---|
Domain includes https:// |
Use your-tenant.auth0.com format only - no scheme prefix |
| Audience doesn't match API Identifier | Must exactly match the API Identifier set in Auth0 Dashboard |
| Created Application instead of API in Auth0 | Must create API resource in Auth0 Dashboard → Applications → APIs |
| Wrong middleware order | UseAuthentication() must come before UseAuthorization() |
| Using ID token instead of access token | Must use access token for API auth, not ID token |
| HTTPS certificate errors locally | Run dotnet dev-certs https --trust |
See Integration Guide for defining and enforcing scope policies.
Built-in proof-of-possession token binding per RFC 9449. See Integration Guide for configuration.
auth0-quickstart - Basic Auth0 setupauth0-mfa - Add Multi-Factor Authenticationauth0-cli - Manage Auth0 resources from the terminalConfiguration Options:
options.Domain - Auth0 tenant domain, no https:// prefix (required)options.JwtBearerOptions.Audience - API Identifier from Auth0 API settings (required)options.JwtBearerOptions - Full access to underlying Microsoft JWT Bearer optionsUser Claims:
ctx.User.FindFirst("sub")?.Value - User ID (subject)ctx.User.FindFirst("scope")?.Value - Space-separated scopesctx.User.FindAll("scope") - All scope claimsCommon Use Cases:
.RequireAuthorization() (see Step 5)[Authorize] attribute (see Step 5)原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。