登入使用者的 Web 應用程式:應用程式註冊
本文說明登入使用者的 Web 應用程式的應用程式註冊步驟。
若要註冊您的應用程式,您可以使用:
- Web 應用程式快速入門。 除了建立應用程式的絕佳第一次體驗之外,Azure 入口網站 中的快速入門還包含名為 [為我進行這項變更] 的按鈕。 您可以使用此按鈕來設定所需的屬性,即使是現有的應用程式也一併設定。 將這些屬性的值調整為您自己的案例。 特別是,您應用程式的 Web API URL 可能會與建議的預設值不同,這也會影響註銷 URI。
- 要手動註冊應用程式的 Azure 入口網站。
- PowerShell 和命令行工具。
使用快速入門註冊應用程式
您可以使用下列連結來啟動建立 Web 應用程式:
使用 Azure 入口網站 註冊應用程式
提示
本文中的步驟可能會根據您從開始的入口網站稍有不同。
注意
要使用的入口網站會根據您的應用程式在 Microsoft Azure 公用雲端或國家或主權雲端中執行而有所不同。 如需詳細資訊,請參閱 國家雲端。
- 登入 Microsoft Entra 系統管理中心。
- 如果您有多個租使用者的存取權,請使用頂端功能表中的 [設定] 圖示,切換至您想要從 [目錄 + 訂用帳戶] 功能表註冊應用程式的租使用者。
- 流覽至 [身分>識別應用程式> 應用程式註冊],選取 [新增註冊]。
- 當 [註冊應用程式] 頁面出現時,請輸入應用程式的註冊資訊:
- 輸入應用程式的名稱,例如
AspNetCore-WebApp
。 您的應用程式使用者可能會看到此名稱,而且您稍後可以加以變更。 - 為您的應用程式選擇支援的帳戶類型。 (請參閱 支援的帳戶類型。)
- 針對 [ 重新導向 URI],新增應用程式類型和 URI 目的地,以在成功驗證之後接受傳回的令牌回應。 例如,輸入
https://localhost:44321
。 - 選取註冊。
- 輸入應用程式的名稱,例如
- 在 [管理] 底下,選取 [驗證],然後新增下列資訊:
- 在 [Web] 區段中,新增
https://localhost:44321/signin-oidc
為重新導向 URI。 - 在 [ 前端通道註銷 URL] 中, 輸入
https://localhost:44321/signout-oidc
。 - 在 [隱含授與和混合式流程] 下,選取 [識別碼權杖]。
- 選取 [儲存]。
- 在 [Web] 區段中,新增
使用 PowerShell 註冊應用程式
您也可以使用 New-MgApplication 向 Microsoft Graph PowerShell 註冊應用程式。
以下是程式代碼的概念。 如需功能完整的程序代碼,請參閱 此範例
# Connect to the Microsoft Graph API, non-interactive is not supported for the moment (Oct 2021)
Write-Host "Connecting to Microsoft Graph"
if ($tenantId -eq "") {
Connect-MgGraph -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
}
else {
Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
}
$context = Get-MgContext
$tenantId = $context.TenantId
# Get the user running the script
$currentUserPrincipalName = $context.Account
$user = Get-MgUser -Filter "UserPrincipalName eq '$($context.Account)'"
# get the tenant we signed in to
$Tenant = Get-MgOrganization
$tenantName = $Tenant.DisplayName
$verifiedDomain = $Tenant.VerifiedDomains | where {$_.Isdefault -eq $true}
$verifiedDomainName = $verifiedDomain.Name
$tenantId = $Tenant.Id
Write-Host ("Connected to Tenant {0} ({1}) as account '{2}'. Domain is '{3}'" -f $Tenant.DisplayName, $Tenant.Id, $currentUserPrincipalName, $verifiedDomainName)
# Create the webApp AAD application
Write-Host "Creating the AAD application (WebApp)"
# create the application
$webAppAadApplication = New-MgApplication -DisplayName "WebApp" `
-Web `
@{ `
RedirectUris = "https://localhost:44321/", "https://localhost:44321/signin-oidc"; `
HomePageUrl = "https://localhost:44321/"; `
LogoutUrl = "https://localhost:44321/signout-oidc"; `
} `
-SignInAudience AzureADandPersonalMicrosoftAccount `
#end of command
$currentAppId = $webAppAadApplication.AppId
$currentAppObjectId = $webAppAadApplication.Id
$tenantName = (Get-MgApplication -ApplicationId $currentAppObjectId).PublisherDomain
#Update-MgApplication -ApplicationId $currentAppObjectId -IdentifierUris @("https://$tenantName/WebApp")
# create the service principal of the newly created application
$webAppServicePrincipal = New-MgServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
# add the user running the script as an app owner if needed
$owner = Get-MgApplicationOwner -ApplicationId $currentAppObjectId
if ($owner -eq $null)
{
New-MgApplicationOwnerByRef -ApplicationId $currentAppObjectId -BodyParameter = @{"@odata.id" = "htps://graph.microsoft.com/v1.0/directoryObjects/$user.ObjectId"}
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
}
Write-Host "Done creating the webApp application (WebApp)"