次の方法で共有


シングルページ アプリケーション: サインインコードとサインアウト コードを追加する

適用対象: 白いチェック マーク記号が付いた緑の円。 従業員テナント 灰色の X 記号が付いた白い円。 外部テナント (詳細はこちら)

アプリケーション内の API にアクセスするトークンを取得する前に、認証されたユーザー コンテキストが必要です。 ユーザーを認証するために、ポップアップ ウィンドウリダイレクト サインイン メソッドを使用することができます。

認証されたユーザー コンテキストまたは ID トークンに、ご利用のアプリケーションからアクセスできる場合は、サインイン手順をスキップして、直接トークンを取得することができます。 詳細については、ユーザー ヒントを使用するシングル サインオン (SSO) に関するページを参照してください。

ポップアップ エクスペリエンスか、リダイレクト エクスペリエンスを選択

ポップアップまたはリダイレクト エクスペリエンスのいずれを選択するかは、ご利用のアプリケーション フローに依存します。

  • 認証中にユーザーにメイン アプリケーション ページから移動してほしくない場合は、ポップアップ ウィンドウを使用します。 認証リダイレクトはポップアップ ウィンドウで行われるため、メイン アプリケーションの状態は保持されます。
  • ユーザーのブラウザーに制約またはポリシーがあり、ポップアップ ウィンドウが無効になっている場合は、リダイレクトを使用します。 たとえば、Internet Explorer のポップアップ ウィンドウには既知の問題があります。

ポップアップ ウィンドウを使用してサインインする

ユーザーがまだサインインしていないときにサインイン エクスペリエンスを呼び出すには、MsalAuthenticationTemplate@azure/msal-react 関数を使用します。 MSAL React ラッパーでは、特定のコンポーネントを MsalAuthenticationTemplate コンポーネントでラップすることによって保護します。

import { InteractionType } from "@azure/msal-browser";
import { MsalAuthenticationTemplate, useMsal } from "@azure/msal-react";

function WelcomeUser() {
  const { accounts } = useMsal();
  const username = accounts[0].username;

  return <p>Welcome, {username}</p>;
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <MsalAuthenticationTemplate interactionType={InteractionType.Popup}>
      <p>This will only render if a user is not signed-in.</p>
      <WelcomeUser />
    </MsalAuthenticationTemplate>
  );
}

ユーザー操作 (ボタンの選択など) に基づいて特定のサインイン エクスペリエンスを呼び出すには、AuthenticatedTemplateUnauthenticatedTemplate@azure/msal-react 関数を使用します。

import {
  useMsal,
  AuthenticatedTemplate,
  UnauthenticatedTemplate,
} from "@azure/msal-react";

function signInClickHandler(instance) {
  instance.loginPopup();
}

// SignInButton Component returns a button that invokes a popup sign in when clicked
function SignInButton() {
  // useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
  const { instance } = useMsal();

  return <button onClick={() => signInClickHandler(instance)}>Sign In</button>;
}

function WelcomeUser() {
  const { accounts } = useMsal();
  const username = accounts[0].username;

  return <p>Welcome, {username}</p>;
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <>
      <AuthenticatedTemplate>
        <p>This will only render if a user is signed-in.</p>
        <WelcomeUser />
      </AuthenticatedTemplate>
      <UnauthenticatedTemplate>
        <p>This will only render if a user is not signed-in.</p>
        <SignInButton />
      </UnauthenticatedTemplate>
    </>
  );
}

リダイレクトを使用してサインインする

ユーザーがサインインしていないときにサインイン エクスペリエンスを呼び出すには、MsalAuthenticationTemplate@azure/msal-react 関数を使用します。

import { InteractionType } from "@azure/msal-browser";
import { MsalAuthenticationTemplate, useMsal } from "@azure/msal-react";

function WelcomeUser() {
  const { accounts } = useMsal();
  const username = accounts[0].username;

  return <p>Welcome, {username}</p>;
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <MsalAuthenticationTemplate interactionType={InteractionType.Redirect}>
      <p>This will only render if a user is not signed-in.</p>
      <WelcomeUser />
    </MsalAuthenticationTemplate>
  );
}

ユーザー操作 (ボタンの選択など) に基づいて特定のサインイン エクスペリエンスを呼び出すには、AuthenticatedTemplateUnauthenticatedTemplate@azure/msal-react 関数を使用します。

import {
  useMsal,
  AuthenticatedTemplate,
  UnauthenticatedTemplate,
} from "@azure/msal-react";

function signInClickHandler(instance) {
  instance.loginRedirect();
}

// SignInButton Component returns a button that invokes a popup login when clicked
function SignInButton() {
  // useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
  const { instance } = useMsal();

  return <button onClick={() => signInClickHandler(instance)}>Sign In</button>;
}

function WelcomeUser() {
  const { accounts } = useMsal();
  const username = accounts[0].username;

  return <p>Welcome, {username}</p>;
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <>
      <AuthenticatedTemplate>
        <p>This will only render if a user is signed-in.</p>
        <WelcomeUser />
      </AuthenticatedTemplate>
      <UnauthenticatedTemplate>
        <p>This will only render if a user is not signed-in.</p>
        <SignInButton />
      </UnauthenticatedTemplate>
    </>
  );
}

ブラウザーでのサインアウト動作

確実に 1 つまたは複数のアプリから安全にサインアウトするには、次のメソッドをお勧めします。

  • 共有デバイスでは、ユーザーはブラウザーのプライベート/シークレット モードを使用し、デバイスから離れる前にすべてのブラウザー ウィンドウを閉じる必要があります。

  • 共有されていないデバイスでは、ユーザーはオペレーティング システムのロック画面を使用して、デバイス上のオペレーティング システム セッション全体をロックまたはサインアウトする必要があります。 Microsoft ではサインアウト ページを使用して、これらのプライバシーとセキュリティのベスト プラクティスをユーザーに通知します。

詳細については、Microsoft の インターネット プライバシーに関するベスト プラクティスを参照してください。

ユーザーが推奨事項を使用してサインアウトしないことを選択した場合に、サインアウト機能を有効にする他のメソッドを以下に示します。

  • フェデレーション サインアウト用の Microsoft の OpenID Connect のフロント チャネル ログアウト。このオプションは、あるアプリが新しいアプリとサインイン状態を共有するものの、独自のセッション トークン/Cookie を管理する場合に使用できます。 ブラウザーでサードパーティの Cookie がブロックされる場合など、コンテンツがブロックされる場合、この実装にはいくつかの制限があります。

  • ローカル アプリのサインアウト用のポップアップ ウィンドウリダイレクト。ポップアップおよびリダイレクト メソッドでは、エンドポイントとローカル アプリでユーザーのセッションが終了します。 しかし、フロント チャネル通信がブロックされている場合、これらのメソッドでは他のフェデレーション アプリケーションのセッションがすぐにクリアされない可能性があります。

ポップアップ ウィンドウを使用してサインアウトする

MSAL.js v2 以上には logoutPopup メソッドが用意されています。これにより、ブラウザー ストレージのキャッシュがクリアされ、Microsoft Entra サインアウト ページへのポップアップ ウィンドウが開かれます。 サインアウト後、リダイレクトは既定でサインインの開始ページに設定され、ポップアップは閉じられます。

サインアウト後のエクスペリエンスでは、ユーザーを特定の URI にリダイレクトするように postLogoutRedirectUri を設定できます。 この URI は、アプリケーションの登録でリダイレクト URI として登録する必要があります。 要求の一部として logoutPopup を渡すことによって、メイン ウィンドウを別のページ (ホーム ページやサインイン ページなど) にリダイレクトするように mainWindowRedirectUri を構成することもできます。

import {
  useMsal,
  AuthenticatedTemplate,
  UnauthenticatedTemplate,
} from "@azure/msal-react";

function signOutClickHandler(instance) {
  const logoutRequest = {
    account: instance.getAccountByHomeId(homeAccountId),
    mainWindowRedirectUri: "your_app_main_window_redirect_uri",
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  };
  instance.logoutPopup(logoutRequest);
}

// SignOutButton component returns a button that invokes a pop-up sign out when clicked
function SignOutButton() {
  // useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
  const { instance } = useMsal();

  return (
    <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
  );
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <>
      <AuthenticatedTemplate>
        <p>This will only render if a user is signed-in.</p>
        <SignOutButton />
      </AuthenticatedTemplate>
      <UnauthenticatedTemplate>
        <p>This will only render if a user is not signed-in.</p>
      </UnauthenticatedTemplate>
    </>
  );
}

リダイレクトを使用してサインアウトする

MSAL.js は、ブラウザー ストレージ内のキャッシュをクリアし、Microsoft Entra サインアウト ページにリダイレクトする v2 の logoutRedirect メソッドを提供します。 サインアウト後、リダイレクトは既定でサインインの開始ページに設定されます。 サインアウト後のエクスペリエンスでは、ユーザーを特定の URI にリダイレクトするように postLogoutRedirectUri を設定できます。 この URI は、アプリケーションの登録でリダイレクト URI として登録する必要があります。

この方法では、プライベート ブラウザーとロック画面の使用に関する インターネット プライバシーのベスト プラクティス に関する Microsoft のリマインダーが表示されないため、ベスト プラクティスを説明し、アプリからサインアウトしたときにすべてのブラウザー ウィンドウを閉じるようユーザーに通知することができます。

import {
  useMsal,
  AuthenticatedTemplate,
  UnauthenticatedTemplate,
} from "@azure/msal-react";

function signOutClickHandler(instance) {
  const logoutRequest = {
    account: instance.getAccountByHomeId(homeAccountId),
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  };
  instance.logoutRedirect(logoutRequest);
}

// SignOutButton Component returns a button that invokes a redirect logout when clicked
function SignOutButton() {
  // useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
  const { instance } = useMsal();

  return (
    <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
  );
}

// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
  return (
    <>
      <AuthenticatedTemplate>
        <p>This will only render if a user is signed-in.</p>
        <SignOutButton />
      </AuthenticatedTemplate>
      <UnauthenticatedTemplate>
        <p>This will only render if a user is not signed-in.</p>
      </UnauthenticatedTemplate>
    </>
  );
}

次のステップ