> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.generaltranslation.app/llms.txt
> Use this file to discover all available pages before exploring further.

> セッションメタデータに組織メタデータを追加する方法を説明します

# ユースケース: セッションメタデータ内の組織情報

<Warning>
  Session Metadata は現在、Enterprise のお客様のみを対象とした早期アクセス段階にあります。この機能を使用することにより、お客様は Okta の [Master Subscription Agreement](https://www.okta.com/legal) に定められた該当する無料トライアル条件に同意したものとみなされます。Auth0 の製品リリースサイクルの詳細については、[Product Release Stages](https://auth0.com/docs/troubleshoot/product-lifecycle/product-release-stages) を参照してください。
</Warning>

Session Metadata によって、ユーザーコンテキストデータはセッションのライフサイクル全体およびログアウトイベントを通じて、ポータブルかつ一貫して参照可能になります。

下流側のシステムは、監査の実施、分析の実行、失効処理パイプラインの適用などの用途で、セッションメタデータ情報を利用できます。

<div id="add-organization-information-to-session-metadata">
  ## セッションメタデータに組織情報を追加する
</div>

[Actions](/docs/ja-JP/customize/actions) を使用して、[post-login](/docs/ja-JP/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger) トリガーで `api.session.setMetadata()` メソッドにより組織の識別子をセッションに保存し、`event.session.metadata` オブジェクトで取得できます。

Post-Login Action のコード:

```javascript lines theme={null}
/**
 * Post-Login Action (シンプル)
 * 組織コンテキストをセッションメタデータに追加し、後続のActions、
 * Management API、および(有効な場合)Back-Channel Logoutトークンに表示されるようにします。
 */
exports.onExecutePostLogin = async (event, api) => {
  // Only proceed if the transaction targets an Organization
  if (!event.organization) return;

  // Keep values short and string-only (session metadata requires strings)
  const orgId = String(event.organization.id || "");
  const orgSlug = String(event.organization.name || "");
  const orgDisplay = String(event.organization.display_name || orgSlug);

  // Minimal, idempotent writes (only a few keys to stay well under limits)
  api.session.setMetadata("org_id", orgId);
  api.session.setMetadata("org_slug", orgSlug);
  api.session.setMetadata("org_name", orgDisplay);
};
```

セッションメタデータは後続の Actions で利用でき、[Management API](https://auth0.com/docs/api/management/v2) を通じて取得できるほか、[OpenID Connect Back-Channel Logout](/docs/ja-JP/authenticate/login/logout/back-channel-logout) トークンに含めることもできます。

* 後続の Actions では、`event.session.metadata` オブジェクト経由でこのデータを参照できます。

```javascript lines theme={null}
const orgId = event.session.metadata?.org_id;
```

* Management API を使用する場合は、[/api/v2/sessions/{id}](https://auth0.com/docs/api/management/v2/sessions/get-session) エンドポイントからデータを取得できます。

```bash lines theme={null}
GET /api/v2/sessions/{id}
```

レスポンス例:

```json lines theme={null}
{
  "session_metadata": {
    "org_id": "org_abc123",
    "org_slug": "acme",
    "org_name": "Acme Corp"
  }
}
```

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  `/api/v2/sessions/{id}` エンドポイントへの呼び出しには、`update:session` スコープを持つ [Management API アクセストークン](/docs/ja-JP/secure/tokens/access-tokens) が必要です。
</Callout>

* [OIDC バックチャネル ログアウト セッションメタデータ](/docs/ja-JP/manage-users/sessions/session-metadata/configure-session-metadata) を構成しているアプリケーションを使用している場合、`logout_token` にセッションメタデータが含まれます。

```json lines theme={null}
{
  "events": { "http://schemas.openid.net/event/backchannel-logout": {} },
  "session_metadata": {
    "org_id": "org_abc123",
    "org_slug": "acme",
    "org_name": "Acme Corp"
  }
}
```
