分享方式:


在 Azure Government 上整合 Microsoft Entra 驗證與 Web Apps

下列快速入門可協助您開始整合 Microsoft Entra 驗證與 Azure Government 上的應用程式。 Azure Government 上的 Microsoft Entra 驗證類似於 Azure 商業平臺,但有少數例外

深入了解 Microsoft Entra 驗證案例

使用 OpenID Connect 將 Microsoft Entra 登入整合到 Web 應用程式中

本節說明如何使用 OpenID Connect 通訊協定整合 Microsoft Entra ID,以將使用者登入 Web 應用程式。

必要條件

步驟 1:向 Microsoft Entra 租用戶註冊 Web 應用程式

  1. 登入 Azure Government 入口網站

  2. 在頂端列上按一下您的帳戶,然後在 [目錄] 清單底下,選擇您要註冊應用程式的 Active Directory 租用戶。

  3. 按一下左側導覽中的 [所有服務],然後選擇 [Microsoft Entra ID]。

  4. 按一下 [應用程式註冊],然後選擇 [新增]

  5. 輸入應用程式的名稱,然後選取 [Web 應用程式和/或 Web API] 作為 [應用程式類型]。 針對登入 URL,輸入應用程式的基底 URL,也就是您的 Azure 應用程式 URL +「/signin-oidc」。

    注意

    如果您尚未部署應用程式,而且想要在本機執行,則應用程式 URL 是本機主機位址。

    按一下 [建立] 來建立應用程式。

  6. 留在 Azure 入口網站中,選擇您的應用程式,按一下 [設定],然後選擇 [屬性]

  7. 找到應用程式識別碼值,並將它複製到剪貼簿。

  8. 針對 [應用程式識別碼 URI],輸入 https://<your_tenant_name>/<name_of_your_app>,將 <your_tenant_name> 取代為您的 Microsoft Entra 租用戶名稱,並將 <name_of_your_app> 取代為您的應用程式的名稱。

步驟 2:設定您的應用程式以使用您的 Microsoft Entra 租用戶

Azure Government 變化

在 Azure Government 雲端上設定 Microsoft Entra 授權時,唯一的變化是在 Microsoft Entra 執行個體中:

  • "https://login.microsoftonline.us"

設定 InventoryApp 專案

  1. 在 Visual Studio 2019 中開啟您的應用程式。

  2. 開啟 appsettings.json 檔案。

  3. 新增 Authentication 區段,並使用您的 Microsoft Entra 租用戶資訊填寫屬性。

    //ClientId: Azure AD->  App registrations -> Application ID
    //Domain: <tenantname>.onmicrosoft.us
    //TenantId: Azure AD -> Properties -> Directory ID
    
    "Authentication": {
        "AzureAd": {
    
        "Azure ADInstance": "https://login.microsoftonline.us/",
        "CallbackPath": "/signin-oidc",
        "ClientId": "<clientid>",
        "Domain": "<domainname>",
        "TenantId": "<tenantid>"
        }
    }
    
  4. 使用 Azure Government 入口網站中應用程式用戶端識別碼來填入 ClientId 屬性。 可以瀏覽至 [Microsoft Entra 識別碼 -> 應用程式註冊 -> 您的應用程式 -> 應用程式識別碼] 來尋找用戶端識別碼。

  5. 使用 Azure Government 入口網站中應用程式組用戶識別碼來填入 TenantId 屬性。 您可以瀏覽至 [Microsoft Entra 識別碼 -> 屬性 -> 目錄 ID] 來尋找租用戶標識符。

  6. 使用 <tenantname>.onmicrosoft.us 填入 Domain 屬性。

  7. 開啟 startup.cs 檔案。

  8. 在您的 ConfigureServices 方法中新增下列程式碼:

        public void ConfigureServices(IServiceCollection services)
        {      
            //Add Azure AD authentication
            services.AddAuthentication(options => {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options => {
                options.Authority = Configuration["Authentication:AzureAd:Azure ADInstance"] + Configuration["Authentication:AzureAd:TenantId"];
                options.ClientId = Configuration["Authentication:AzureAd:ClientId"];
                options.CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"];
            });
    
        }
    

    在相同檔案中,將這一行程式代碼新增至 Configure 方法:

    app.UseAuthentication();
    
  9. 瀏覽至您期望您的使用者登入首頁控制器或作為首頁的任意控制器檔案。 在類別定義之前新增 [Authorize] 標籤。

下一步