用于登录用户的 Web 应用:应用注册

文本介绍用于登录用户的 Web 应用的应用注册步骤。

若要注册应用程序,可以使用:

  • Web 应用快速入门。 除了提供创建应用程序的第一手体验,Azure 门户中的快速入门还包含名为“为我进行此更改”的按钮。 可以使用此按钮设置所需属性,对现有应用也可以这样做。 根据自己的情况调整这些属性的值。 具体而言,应用的 Web API URL 可能会不同于建议的默认值,后者还会影响注销 URI。
  • 用于手动注册应用程序的 Azure 门户。
  • PowerShell 和命令行工具。

按照快速入门注册应用

可以使用以下链接启动 Web 应用程序的创建:

注册应用程序

使用 Azure 门户注册应用程序

提示

本文中的步骤可能因开始使用的门户而略有不同。

注意

要使用的门户根据应用程序是在 Azure 公有云中运行还是在国家云或主权云中运行而异。 有关详细信息,请参阅国家云

  1. 登录 Microsoft Entra 管理中心
  2. 如果你有权访问多个租户,请使用顶部菜单中的“设置”图标 ,通过“目录 + 订阅”菜单切换到你希望在其中注册应用程序的租户。
  3. 浏览至“标识”>“应用程序”>“应用注册”,然后选择“新建注册”。
  1. “注册应用程序”页出现后,请输入应用程序的注册信息:
    1. 输入应用程序的名称(例如 AspNetCore-WebApp)。 应用的用户可能会看到此名称,你稍后可对其进行更改。
    2. 为应用程序选择支持的帐户类型。 (请参阅支持的帐户类型。)
    3. 在“重定向 URI”中,添加将在成功进行身份验证后接受返回的令牌响应的应用程序类型和 URI 目标。 例如,输入 https://localhost:44321
    4. 选择“注册”。
  2. 在“管理”下,选择“身份验证”,然后添加以下信息:
    1. 在“Web”部分,添加 https://localhost:44321/signin-oidc 作为“重定向 URI” 。
    2. 在前通道注销 URL 中,输入 https://localhost:44321/signout-oidc
    3. 在“隐式授权和混合流”下,选择“ID 令牌” 。
    4. 选择“保存”。

使用 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 -Environment China -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
}
else {
   Connect-MgGraph -Environment China -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 AzureADMultipleOrgs `
                                                   #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://microsoftgraph.chinacloudapi.cn/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)"

后续步骤

转到此方案中的下一篇文章:应用的代码配置