驗證和授權靜態 Web Apps

警告

由於 X(先前稱為 Twitter) API 原則的變更,我們無法繼續支援它,作為您應用程式預先設定提供者的一部分。 如果您想要繼續使用 X(先前稱為 Twitter)向您的應用程式進行驗證/授權,請更新應用程式組態以 註冊自定義提供者

Azure Static Web Apps 提供簡化的驗證體驗,不需要其他動作或設定,即可使用 GitHub 和 Microsoft Entra ID 進行驗證。

在本文中,瞭解預設行為、如何設定登入和註銷、如何封鎖驗證提供者等等。

您可以 註冊自定義提供者,這會停用所有預先設定的提供者。

必要條件

請注意下列使用 Azure Static Web Apps 進行驗證和授權的預設值和資源。

違約:

  • 任何使用者都可以向預先設定的提供者進行驗證
  • 登入之後,用戶屬於 anonymousauthenticated 角色。 如需角色的詳細資訊,請參閱 管理角色

資源:

設定登入

Azure Static Web Apps 使用 /.auth 系統資料夾來提供授權相關 API 的存取權。 不要將資料夾下 /.auth 的任何路由直接公開給使用者,而是建立 易記 URL 的路由規則

使用下表來尋找提供者特定的路由。

授權提供者 登入路由
Microsoft Entra ID /.auth/login/aad
GitHub /.auth/login/github

例如,若要使用 GitHub 登入,您可以包含類似下列連結的內容。

<a href="/.auth/login/github">Login</a>

如果您選擇支援多個提供者,請在您的網站上公開每個提供者的特定連結。 使用路由規則,將預設提供者對應至類似 /login易記路由。

{
  "route": "/login",
  "redirect": "/.auth/login/github"
}

設定登入后重新導向

藉由在查詢字串參數中 post_login_redirect_uri 提供完整 URL,將用戶傳回特定頁面,如下列範例所示。

<a href="/.auth/login/github?post_login_redirect_uri=https://zealous-water.azurestaticapps.net/success">Login</a>

You can also redirect unauthenticated users back to the referring page after they sign in. To configure this behavior, create a response override rule that sets post_login_redirect_uri to .referrer, like in the following example.

{
  "responseOverrides": {
    "401": {
      "redirect": "/.auth/login/github?post_login_redirect_uri=.referrer",
      "statusCode": 302
    }
  }
}

設定註銷

路由 /.auth/logout 會將使用者從網站註銷。 您可以新增網站導覽的連結,讓用戶註銷,如下列範例所示。

<a href="/.auth/logout">Log out</a>

使用路由規則來對應類似 /logout易記路由。

{
  "route": "/logout",
  "redirect": "/.auth/logout"
}

設定註銷後重新導向

若要在使用者註銷後將用戶傳回特定頁面,請在查詢字串參數中 post_logout_redirect_uri 提供 URL。

封鎖驗證提供者

您可能想要限制應用程式使用驗證提供者,因為已啟用所有驗證提供者。 例如,您的應用程式可能只想在公開電子郵件位址的提供者上標準化。

若要封鎖提供者,您可以建立 路由規則 ,以針對封鎖提供者特定路由的要求傳回 404 狀態代碼。 例如,若要將 Twitter 限制為提供者,請新增下列路由規則。

{
  "route": "/.auth/login/twitter",
  "statusCode": 404
}

拿掉個人資料

當您以使用者身分授與應用程式同意時,應用程式可以存取您的電子郵件地址或使用者名稱,視身分識別提供者而定。 一旦提供這項資訊,應用程式的擁有者就可以決定如何管理個人資料。

用戶必須連絡個別 Web 應用程式的系統管理員,以從其系統撤銷此資訊。

若要從 Azure Static Web Apps 平臺移除個人資料,並防止平臺針對未來的要求提供這項資訊,請使用下列 URL 提交要求:

https://identity.azurestaticapps.net/.auth/purge/<AUTHENTICATION_PROVIDER_NAME>

To prevent the platform from providing this information on future requests to individual apps, submit a request using the following URL:

https://<WEB_APP_DOMAIN_NAME>/.auth/purge/<AUTHENTICATION_PROVIDER_NAME>

If you're using Microsoft Entra ID, use aad as the value for the <AUTHENTICATION_PROVIDER_NAME> placeholder.

Tip

For information about general restrictions and limitations, see Quotas.

後續步驟