單頁應用程式:登入和註銷

在取得令牌以存取應用程式中的 API 之前,您需要已驗證的用戶內容。 若要驗證使用者,您可以使用 彈出視窗 和/或 重新導向 登入方法。

如果您的應用程式可以存取已驗證的用戶內容或標識元令牌,您可以略過登入步驟,並直接取得令牌。 如需詳細資訊,請參閱 具有使用者提示的單一登錄 (SSO)。

選擇快顯或重新導向體驗

快顯或重新導向體驗之間的選擇取決於您的應用程式流程。

  • 如果您不想讓使用者在驗證期間離開主應用程式頁面,請使用彈出視窗。 因為驗證重新導向會在彈出視窗中發生,因此會保留主要應用程式的狀態。

  • 如果使用者有停用彈出視窗的瀏覽器條件約束或原則,請使用重新導向。 例如,Internet Explorer 上的彈出視窗有已知問題。

使用彈出視窗登入

const config = {
  auth: {
    clientId: "your_app_id",
    redirectUri: "your_app_redirect_uri", //defaults to application start page
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  },
};

const loginRequest = {
  scopes: ["User.ReadWrite"],
};

let accountId = "";

const myMsal = new PublicClientApplication(config);

myMsal
  .loginPopup(loginRequest)
  .then(function (loginResponse) {
    accountId = loginResponse.account.homeAccountId;
    // Display signed-in user content, call API, etc.
  })
  .catch(function (error) {
    //login failure
    console.log(error);
  });

使用重新導向登入

const config = {
  auth: {
    clientId: "your_app_id",
    redirectUri: "your_app_redirect_uri", //defaults to application start page
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  },
};

const loginRequest = {
  scopes: ["User.ReadWrite"],
};

let accountId = "";

const myMsal = new PublicClientApplication(config);

function handleResponse(response) {
  if (response !== null) {
    accountId = response.account.homeAccountId;
    // Display signed-in user content, call API, etc.
  } else {
    // In case multiple accounts exist, you can select
    const currentAccounts = myMsal.getAllAccounts();

    if (currentAccounts.length === 0) {
      // no accounts signed-in, attempt to sign a user in
      myMsal.loginRedirect(loginRequest);
    } else if (currentAccounts.length > 1) {
      // Add choose account code here
    } else if (currentAccounts.length === 1) {
      accountId = currentAccounts[0].homeAccountId;
    }
  }
}

myMsal.handleRedirectPromise().then(handleResponse);

瀏覽器上的註銷行為

若要確保安全註銷一或多個應用程式,建議使用下列方法:

  • 在共用裝置上,用戶應該使用瀏覽器的私人/incognito 模式,並關閉所有瀏覽器視窗,然後才離開裝置。

  • 在未共享的裝置上,用戶應該使用操作系統鎖定畫面,在裝置上鎖定或註銷其整個操作系統會話。 Microsoft 會使用其註銷頁面提醒使用者這些隱私權和安全性最佳做法。

如需詳細資訊,請參閱 Microsoft 的 因特網隱私權最佳做法

如果使用者選擇不使用建議註銷,以下是啟用註銷功能的其他方法:

  • Microsoft 的 OpenID 連線 同盟註銷的 Front Channel Logout。當應用程式與新應用程式共用登入狀態,但管理自己的會話令牌/Cookie 時,您可以使用此選項。 此實作有一些限制,其中內容遭到封鎖,例如瀏覽器封鎖第三方 Cookie 時。

  • 彈出視窗和/或本機應用程式註銷的重新導向。快顯和重新導向方法會在端點和本機應用程式端結束使用者的會話。 但是,如果封鎖前端通道通訊,這些方法可能不會立即清除其他同盟應用程式的會話。

使用彈出視窗註銷

MSAL.js v2 和更新版本提供一種方法 logoutPopup ,以清除瀏覽器記憶體中的快取,並開啟 Microsoft Entra 註銷頁面的彈出視窗。 註銷之後,重新導向預設為登入起始頁面,並關閉快顯。

針對註銷后的體驗,您可以將 設定 postLogoutRedirectUri 為將使用者重新導向至特定 URI。 此 URI 應該在應用程式註冊中註冊為重新導向 URI。 您也可以將 logoutPopup 主視窗重新導向至不同的頁面,例如首頁或登入頁面,方法是在要求中傳遞 mainWindowRedirectUri

const config = {
  auth: {
    clientId: "your_app_id",
    redirectUri: "your_app_redirect_uri", // defaults to application start page
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  },
};

const myMsal = new PublicClientApplication(config);

// you can select which account application should sign out
const logoutRequest = {
  account: myMsal.getAccountByHomeId(homeAccountId),
  mainWindowRedirectUri: "your_app_main_window_redirect_uri",
};

await myMsal.logoutPopup(logoutRequest);

使用重新導向註銷

MSAL.js提供 logout v1 中的方法,以及 logoutRedirect v2 中清除瀏覽器記憶體中的快取,並重新導向至 Microsoft Entra 註銷頁面的方法。 註銷之後,重新導向預設為登入起始頁。

針對註銷后的體驗,您可以將 設定 postLogoutRedirectUri 為將使用者重新導向至特定 URI。 此 URI 應該在應用程式註冊中註冊為重新導向 URI。

因為 Microsoft 提醒使用私人瀏覽器和鎖定畫面的因特網隱私權最佳做法 並未顯示在此方法中,因此您可能會想要描述最佳做法,並提醒使用者關閉所有瀏覽器視窗。

const config = {
  auth: {
    clientId: "your_app_id",
    redirectUri: "your_app_redirect_uri", //defaults to application start page
    postLogoutRedirectUri: "your_app_logout_redirect_uri",
  },
};

const myMsal = new PublicClientApplication(config);

// you can select which account application should sign out
const logoutRequest = {
  account: myMsal.getAccountByHomeId(homeAccountId),
};

myMsal.logoutRedirect(logoutRequest);

下一步

請移至此案例中的下一篇文章, 取得應用程式的令牌。