為 Azure Stack Hub 資源提供應用程式存取

透過 Azure Resource Manager 部署或設定資源的應用程式必須出示自己的身分識別 (稱為安全性主體)。 如同使用者會以使用者主體來表示,應用程式會以服務主體來表示。

身分識別也可以用來只將必要的權限委派給使用者或應用程式。 例如,設定管理應用程式可能會使用 Azure Resource Manager 來清查 Azure 資源。 應用程式會在目錄中註冊,然後新增至適當範圍的「讀者」角色,將應用程式限制為唯讀存取。

概觀

正如同使用者,應用程式必須在驗證期間出示認證,這需要兩個元素:

  • 應用程式識別碼,有時也稱為用戶端識別碼。 可唯一識別 Active Directory 租用戶中的應用程式註冊的 GUID。
  • 祕密。 您可以產生用戶端祕密字串 (類似於密碼),也可以指定 X509 憑證指紋 (使用其公開金鑰)。

以應用程式本身的身分識別加以執行,優於以使用者的身分識別執行應用程式,原因如下:

  • 認證較強 - 應用程式可使用 X509 憑證登入,而不是文字共用祕密/密碼。
  • 可將較嚴格的權限指派給應用程式。 一般而言,這些權限會限制為只能進行應用程式需要進行的動作,也就是所謂的「最低權限原則」。
  • 應用程式的認證和權限變更頻率不像使用者認證那麼高。 例如,當使用者的職責變更、密碼需求規定要變更,或使用者從公司離職時。

首先請在目錄中建立新的應用程式註冊,這會建立相關聯的服務主體物件來代表應用程式在目錄內的身分識別。 註冊程序會根據您為 Azure Stack Hub 執行個體選擇的目錄而有所不同:

  • Microsoft Entra 識別碼:Microsoft Entra 標識符是多租用戶、雲端式、目錄和身分識別管理服務。 您可以搭配連線的 Azure Stack Hub 實例使用 Microsoft Entra 識別碼。 稍後顯示的範例會使用 Azure 入口網站 來註冊應用程式 Microsoft Entra。
  • Active Directory Federation Services (AD FS):AD FS 提供簡化、安全的識別身分同盟及網頁單一登入 (SSO) 功能。 您可以將 AD FS 與已連線和中斷連線的 Azure Stack Hub 執行個體搭配使用。 稍後提供的範例將使用 Azure Stack Hub PowerShell 來進行 AD FS 應用程式註冊。

註冊應用程式之後,您會了解如何將其指派給角色,並限制其資源存取。

管理 Microsoft Entra 應用程式

如果您已將具有 Microsoft Entra 標識符的 Azure Stack Hub 部署為身分識別管理服務,您可以建立和管理應用程式的身分識別,就像您為 Azure 所做的一樣。 本節說明如何使用 Azure 入口網站執行這些步驟。 請在開始之前檢閱註冊應用程式所需的權限,以確定您有足夠的權限可以註冊應用程式。

建立會使用用戶端祕密認證的應用程式註冊

在本節中,您會使用 Azure 入口網站 在 Microsoft Entra 租用戶中註冊您的應用程式。 在下列範例中,您會指定用戶端祕密認證,但入口網站也支援 X509 憑證型認證。

  1. 使用 Azure 帳戶登入 Azure 入口網站

  2. 選取 [Microsoft Entra 標識符>應用程式註冊>[新增註冊]。

  3. 提供應用程式的名稱

  4. 選取適當的 [支援的帳戶類型]。

  5. 在 [重新導向 URI] 底下,選取 [Web] 作為應用程式類型,並 (選擇性地) 指定重新導向 URI (如果應用程式有此需要)。

  6. 設定值之後,選取 [註冊]。 隨即會建立應用程式註冊,並顯示 [概觀] 頁面。

  7. 複製 [應用程式識別碼] 以便用於應用程式的程式碼中。 此值也稱為「用戶端識別碼」。

  8. 若要產生用戶端密碼,請選取 [憑證和祕密] 頁面。 選取 [新增用戶端密碼]。

  9. 提供祕密的說明,以及到期持續時間。

  10. 完成時,選取 [新增] 。

  11. 隨即會顯示祕密的值。 複製此值並儲存到其他位置,否則之後就無法再擷取。 登入期間,您必須在用戶端應用程式中提供祕密與應用程式識別碼。

    用戶端密碼中所儲存的金鑰

請繼續進行指派角色,以了解如何為應用程式的身分識別建立角色型存取控制。

其他 Microsoft Entra 應用程式管理文章

如需管理 Microsoft Entra 應用程式的詳細資訊,請參閱下列 Azure 文章:

管理 AD FS 應用程式

如果您已部署 Azure Stack Hub 與 AD FS 作為身分識別管理服務,就必須使用 PowerShell 來管理應用程式的身分識別。 下列範例會示範 X509 憑證和用戶端祕密認證。

指令碼必須在提升權限 (「以系統管理員身分執行」) 的 PowerShell 主控台中執行,以開啟另一個工作階段來連線至裝載了 Azure Stack Hub 執行個體特殊權限端點的 VM。 一旦建立特殊權限端點工作階段,就會使用其他 Cmdlet 來建立和管理應用程式註冊。 如需有關具特殊權限端點的詳細資訊,請參閱使用 Azure Stack Hub 中具有特殊權限的端點

建立使用憑證認證的應用程式註冊

在建立憑證認證時,必須符合下列需求:

  • 針對生產環境,憑證必須由內部憑證授權單位或公用憑證授權單位發出。 使用公用授權單位時,您必須將授權單位作為 Microsoft 受信任的根授權單位方案的一部分,包含在基礎作業系統映像中。 如需完整清單,請參閱參與者清單 - Microsoft 受信任的根憑證方案。 稍後在更新憑證認證期間,也會顯示建立「自我簽署」測試憑證的範例。
  • 密碼編譯提供者必須指定為 Microsoft 舊版密碼編譯服務提供者 (CSP) 金鑰提供者。
  • 憑證格式必須是 PFX 檔案,因為需要公用與私密金鑰。 Windows 伺服器會使用包含公用金鑰檔案 (TLS/SSL 憑證檔案) 及相關私密金鑰檔案的 .pfx 檔案。
  • 您的 Azure Stack Hub 基礎結構必須能透過網路來存取憑證中所發佈憑證授權單位的憑證撤銷清單 (CRL) 位置。 這個 CRL 必須是 HTTP 端點。

擁有憑證後,請使用下列 PowerShell 指令碼來註冊您的應用程式,並使用應用程式的身分識別登入。 以您自己的值取代下列預留位置:

預留位置 描述 範例
<PepVM> Azure Stack Hub 執行個體上特殊權限端點 VM 的名稱。 "AzS-ERCS01"
<YourCertificateLocation> X509 憑證在本機憑證存放區中的位置。 "Cert:\CurrentUser\My\AB5A8A3533CC7AA2025BF05120117E06DE407B34"
<YourAppName> 新應用程式註冊的描述性名稱。 "My management tool"
  1. 開啟已提升權限的 Windows PowerShell 工作階段,然後執行下列指令碼。

    # Sign in to PowerShell interactively, using credentials that have access to the VM running the Privileged Endpoint (typically <domain>\cloudadmin)
    $Creds = Get-Credential
    
    # Create a PSSession to the Privileged Endpoint VM
    $Session = New-PSSession -ComputerName "<PepVm>" -ConfigurationName PrivilegedEndpoint -Credential $Creds -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US)
    
    # To use a managed certificate from the certificate store, use the Get-Item cmdlet.
    # To use a certificate file, use Get-Certificate for a .cer file, or Get-PfxCertificate for a .pfx file.
    # To use a test certificate, use the New-SelfSignedCertificate cmdlet
    #   See https://learn.microsoft.com/powershell/module/pki/new-selfsignedcertificate for usage details, including using the -Provider parameter
    #   $Cert = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=<YourAppName>" -KeySpec KeyExchange
    $Cert = Get-Item "<YourCertificateLocation>"
    
    # Use the privileged endpoint to create the new app registration
    $SpObject = Invoke-Command -Session $Session -ScriptBlock {New-GraphApplication -Name "<YourAppName>" -ClientCertificates $using:cert}
    $AzureStackInfo = Invoke-Command -Session $Session -ScriptBlock {Get-AzureStackStampInformation}
    $Session | Remove-PSSession
    
    # Using the stamp info for your Azure Stack Hub instance, populate the following variables:
    # - Az endpoint used for Azure Resource Manager operations 
    # - Audience for acquiring an OAuth token used to access Graph API 
    # - GUID of the directory tenant
    $ArmEndpoint = $AzureStackInfo.TenantExternalEndpoints.TenantResourceManager
    $GraphAudience = "https://graph." + $AzureStackInfo.ExternalDomainFQDN + "/"
    $TenantID = $AzureStackInfo.AADTenantID
    
    # Register and set an Az environment that targets your Azure Stack Hub instance
    Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
    
    # Sign in using the new service principal
    $SpSignin = Connect-AzAccount -Environment "AzureStackUser" `
    -ServicePrincipal `
    -CertificateThumbprint $SpObject.Thumbprint `
    -ApplicationId $SpObject.ClientId `
    -TenantId $TenantID
    
    # Output the service principal details
    $SpObject
    
    
  2. 指令碼完成後,其會顯示應用程式註冊資訊。 ClientIDThumbprint 在經過驗證後,將獲得授權而可存取 Azure Resource Manager 所管理的資源。

    ApplicationIdentifier : S-1-5-21-1512385356-3796245103-1243299919-1356
    ClientId              : 3c87e710-9f91-420b-b009-31fa9e430145
    Thumbprint            : 30202C11BE6864437B64CE36C8D988442082A0F1
    ApplicationName       : Azurestack-MyApp-c30febe7-1311-4fd8-9077-3d869db28342
    ClientSecret          :
    PSComputerName        : azs-ercs01
    RunspaceId            : a78c76bb-8cae-4db4-a45a-c1420613e01b
    

請讓 PowerShell 主控台工作階段保持開啟,以便在下一節與 ApplicationIdentifier 值搭配使用。

更新憑證認證

現在您已註冊應用程式,本節將說明如何:

  1. 建立新的自我簽署 X509 憑證以便用於測試。
  2. 更新應用程式的認證,方法是更新其指紋屬性,使其符合新的憑證。

使用 PowerShell 更新憑證認證,並以您自己的值取代下列預留位置:

預留位置 描述 範例
<PepVM> Azure Stack Hub 執行個體上特殊權限端點 VM 的名稱。 "AzS-ERCS01"
<YourAppName> 新應用程式註冊的描述性名稱。 "My management tool"
<YourCertificateLocation> X509 憑證在本機憑證存放區中的位置。 "Cert:\CurrentUser\My\AB5A8A3533CC7AA2025BF05120117E06DE407B34"
<AppIdentifier> 指派給應用程式註冊的識別碼。 "S-1-5-21-1512385356-3796245103-1243299919-1356"
  1. 使用已提升權限的 Windows PowerShell 工作階段來執行下列 Cmdlet:

    # Create a PSSession to the PrivilegedEndpoint VM
    $Session = New-PSSession -ComputerName "<PepVM>" -ConfigurationName PrivilegedEndpoint -Credential $Creds -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US)
    
    # Create a self-signed certificate for testing purposes, using the New-SelfSignedCertificate cmdlet 
    # See https://learn.microsoft.com/powershell/module/pki/new-selfsignedcertificate for usage details, including using the -Provider parameter
    $NewCert = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=<YourAppName>" -KeySpec KeyExchange
    # In production, use Get-Item to retrieve a managed certificate from the certificate store.
    # Alteratively, use Get-Certificate for a .cer file, or Get-PfxCertificate for a .pfx file.
    # $Cert = Get-Item "<YourCertificateLocation>"
    
    # Use the privileged endpoint to update the certificate thumbprint, used by <AppIdentifier>
    $SpObject = Invoke-Command -Session $Session -ScriptBlock {Set-GraphApplication -ApplicationIdentifier "<AppIdentifier>" -ClientCertificates $using:NewCert}
    $Session | Remove-PSSession
    
    # Output the updated service principal details
    $SpObject
    
  2. 指令碼完成後,便會顯示更新後的應用程式註冊資訊,包括新自我簽署憑證的指紋值。

    ApplicationIdentifier : S-1-5-21-1512385356-3796245103-1243299919-1356
    ClientId              : 
    Thumbprint            : AF22EE716909041055A01FE6C6F5C5CDE78948E9
    ApplicationName       : Azurestack-MyApp-c30febe7-1311-4fd8-9077-3d869db28342
    ClientSecret          : 
    PSComputerName        : azs-ercs01
    RunspaceId            : a580f894-8f9b-40ee-aa10-77d4d142b4e5
    

建立會使用用戶端祕密認證的應用程式註冊

警告

使用用戶端密碼的安全性不如使用 X509 憑證認證。 其不只是驗證機制較不安全,一般還必須在用戶端應用程式的原始程式碼中內嵌祕密。 因此對於生產應用程式來說,強烈建議您使用憑證認證。

現在您會建立另一個應用程式註冊,但這次請指定用戶端密碼認證。 不同於憑證認證,目錄能夠產生用戶端密碼認證。 您不必指定用戶端密碼,而是會使用 -GenerateClientSecret 參數來要求產生用戶端密碼。 以您自己的值取代下列預留位置:

預留位置 描述 範例
<PepVM> Azure Stack Hub 執行個體上特殊權限端點 VM 的名稱。 "AzS-ERCS01"
<YourAppName> 新應用程式註冊的描述性名稱。 "My management tool"
  1. 開啟已提升權限的 Windows PowerShell 工作階段,然後執行下列 Cmdlet:

    # Sign in to PowerShell interactively, using credentials that have access to the VM running the Privileged Endpoint (typically <domain>\cloudadmin)
    $Creds = Get-Credential
    
    # Create a PSSession to the Privileged Endpoint VM
    $Session = New-PSSession -ComputerName "<PepVM>" -ConfigurationName PrivilegedEndpoint -Credential $Creds -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US)
    
    # Use the privileged endpoint to create the new app registration
    $SpObject = Invoke-Command -Session $Session -ScriptBlock {New-GraphApplication -Name "<YourAppName>" -GenerateClientSecret}
    $AzureStackInfo = Invoke-Command -Session $Session -ScriptBlock {Get-AzureStackStampInformation}
    $Session | Remove-PSSession
    
    # Using the stamp info for your Azure Stack Hub instance, populate the following variables:
    # - Az endpoint used for Azure Resource Manager operations 
    # - Audience for acquiring an OAuth token used to access Graph API 
    # - GUID of the directory tenant
    $ArmEndpoint = $AzureStackInfo.TenantExternalEndpoints.TenantResourceManager
    $GraphAudience = "https://graph." + $AzureStackInfo.ExternalDomainFQDN + "/"
    $TenantID = $AzureStackInfo.AADTenantID
    
    # Register and set an Az environment that targets your Azure Stack Hub instance
    Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
    
    # Sign in using the new service principal
    $securePassword = $SpObject.ClientSecret | ConvertTo-SecureString -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SpObject.ClientId, $securePassword
    $SpSignin = Connect-AzAccount -Environment "AzureStackUser" -ServicePrincipal -Credential $credential -TenantId $TenantID
    
    # Output the service principal details
    $SpObject
    
  2. 指令碼完成後,其會顯示應用程式註冊資訊。 ClientIDClientSecret 在經過驗證後,將獲得授權而可存取 Azure Resource Manager 所管理的資源。

    ApplicationIdentifier : S-1-5-21-1634563105-1224503876-2692824315-2623
    ClientId              : 8e0ffd12-26c8-4178-a74b-f26bd28db601
    Thumbprint            : 
    ApplicationName       : Azurestack-YourApp-6967581b-497e-4f5a-87b5-0c8d01a9f146
    ClientSecret          : 6RUWLRoBw3EebBLgaWGiowCkoko5_j_ujIPjA8dS
    PSComputerName        : azs-ercs01
    RunspaceId            : 286daaa1-c9a6-4176-a1a8-03f543f90998
    

請讓 PowerShell 主控台工作階段保持開啟,以便在下一節與 ApplicationIdentifier 值搭配使用。

更新用戶端祕密認證

使用 PowerShell 以 ResetClientSecret 參數更新用戶端密碼認證,以立即變更用戶端密碼。 以您自己的值取代下列預留位置:

預留位置 描述 範例
<PepVM> Azure Stack Hub 執行個體上特殊權限端點 VM 的名稱。 "AzS-ERCS01"
<AppIdentifier> 指派給應用程式註冊的識別碼。 "S-1-5-21-1634563105-1224503876-2692824315-2623"
  1. 使用已提升權限的 Windows PowerShell 工作階段來執行下列 Cmdlet:

    # Create a PSSession to the PrivilegedEndpoint VM
    $Session = New-PSSession -ComputerName "<PepVM>" -ConfigurationName PrivilegedEndpoint -Credential $Creds -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US)
    
    # Use the privileged endpoint to update the client secret, used by <AppIdentifier>
    $SpObject = Invoke-Command -Session $Session -ScriptBlock {Set-GraphApplication -ApplicationIdentifier "<AppIdentifier>" -ResetClientSecret}
    $Session | Remove-PSSession
    
    # Output the updated service principal details
    $SpObject
    
  2. 指令碼完成後,便會顯示更新後的應用程式註冊資訊,包括新產生的用戶端密碼。

    ApplicationIdentifier : S-1-5-21-1634563105-1224503876-2692824315-2623
    ClientId              : 8e0ffd12-26c8-4178-a74b-f26bd28db601
    Thumbprint            : 
    ApplicationName       : Azurestack-YourApp-6967581b-497e-4f5a-87b5-0c8d01a9f146
    ClientSecret          : MKUNzeL6PwmlhWdHB59c25WDDZlJ1A6IWzwgv_Kn
    PSComputerName        : azs-ercs01
    RunspaceId            : 6ed9f903-f1be-44e3-9fef-e7e0e3f48564
    

移除應用程式註冊

現在您將了解如何使用 PowerShell 從您的目錄移除應用程式註冊。

以您自己的值取代下列預留位置:

預留位置 描述 範例
<PepVM> Azure Stack Hub 執行個體上特殊權限端點 VM 的名稱。 "AzS-ERCS01"
<AppIdentifier> 指派給應用程式註冊的識別碼。 "S-1-5-21-1634563105-1224503876-2692824315-2623"
# Sign in to PowerShell interactively, using credentials that have access to the VM running the Privileged Endpoint (typically <domain>\cloudadmin)
$Creds = Get-Credential

# Create a PSSession to the PrivilegedEndpoint VM
$Session = New-PSSession -ComputerName "<PepVM>" -ConfigurationName PrivilegedEndpoint -Credential $Creds -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US)

# OPTIONAL: Use the privileged endpoint to get a list of applications registered in AD FS
$AppList = Invoke-Command -Session $Session -ScriptBlock {Get-GraphApplication}

# Use the privileged endpoint to remove application <AppIdentifier>
Invoke-Command -Session $Session -ScriptBlock {Remove-GraphApplication -ApplicationIdentifier "<AppIdentifier>"}

在特殊權限端點上呼叫 Remove-GraphApplication Cmdlet 不會傳回任何輸出,但在執行 Cmdlet 期間,您會看到一字不漏的確認內容輸出到主控台:

VERBOSE: Deleting graph application with identifier S-1-5-21-1634563105-1224503876-2692824315-2623.
VERBOSE: Remove-GraphApplication : BEGIN on AZS-ADFS01 on ADFSGraphEndpoint
VERBOSE: Application with identifier S-1-5-21-1634563105-1224503876-2692824315-2623 was deleted.
VERBOSE: Remove-GraphApplication : END on AZS-ADFS01 under ADFSGraphEndpoint configuration

指派角色

您可以透過角色型存取控制 (RBAC) 來授權使用者和應用程式存取 Azure 資源。 若要讓應用程式能夠存取您訂用帳戶中的資源,您必須將其服務主體「指派」給某個「角色」以存取特定「資源」。 請先決定哪個角色代表應用程式的正確「權限」。 若要了解可用的角色,請參閱 Azure 資源的內建角色

您選擇的資源類型也會建立適用於該應用程式的「存取範圍」。 您可以將存取範圍設定在訂用帳戶、資源群組或資源的層級。 較低的範圍層級會繼承較高層級的權限。 例如,為資源群組的「讀取者」角色新增應用程式,代表該角色可以讀取資源群組及其所包含的任何資源。

  1. 根據您在 Azure Stack Hub 安裝期間指定的目錄登入適當的入口網站, (Microsoft Entra 標識碼的 Azure 入口網站,或 AD FS 的 Azure Stack Hub 使用者入口網站,例如) 。 在此範例中,我們會示範讓使用者登入 Azure Stack Hub 使用者入口網站。

    注意

    若要新增給定資源的角色指派,使用者帳戶必須屬於會宣告 Microsoft.Authorization/roleAssignments/write 權限的角色。 例如,擁有者使用者存取系統管理員內建角色。

  2. 瀏覽至您想要允許應用程式存取的資源。 在此範例中,請將應用程式指派給訂用帳戶範圍的角色,方法是選取 [訂用帳戶],再選取特定訂用帳戶。 您也可以改為選取資源群組,或是虛擬機器之類的特定資源。

    選取要指派的訂用帳戶

  3. 選取 [存取控制 (IAM)] 頁面,只要是支援 RBAC 的資源,就會有此通用頁面。

  4. 選取 [+ 新增]

  5. 在 [角色] 底下,挑選您想要將應用程式指派給哪個角色。

  6. 在 [選取] 底下,使用完整或部分的應用程式名稱來搜尋應用程式。 在註冊期間,應用程式名稱會產生為 Azurestack-YourAppName-GUID<><>。 例如,如果您在建立期間使用了 App2的應用程式名稱,而 GUID 2bbe67d8-3fdb-4b62-87cf-cc41dd4344ff ,則完整名稱會是 Azurestack-App2-2bbe67d8-3fdb-4b62-87cf-cc41dd4344ff。 您可以搜尋確切字串,也可以只搜尋其中的一部分,例如「Azurestack」或「Azurestack-App2」。

  7. 找到應用程式後請加以選取,其便會顯示在 [選取的成員] 底下。

  8. 選取 [儲存] 以完成角色指派。

    指派角色

  9. 完成時,應用程式便會顯示在指派給目前範圍、適用於給定角色的主體清單。

    指派的角色

現在您已為應用程式提供身分識別,並為其授與資源存取權,接下來您可以啟用指令碼或程式碼,以進行登入並安全地存取 Azure Stack Hub 資源。

後續步驟

管理使用者權限
Microsoft Entra 檔
Active Directory 同盟服務 \(英文\)