Skip to content

Commit 4bfb2d6

Browse files
authored
Merge pull request #220689 from jmprieur/fix91931
Fix 91931
2 parents 2586a9c + 3f8313e commit 4bfb2d6

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

articles/active-directory/develop/scenario-web-app-sign-user-app-registration.md

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: conceptual
1111
ms.workload: identity
12-
ms.date: 07/14/2020
12+
ms.date: 12/6/2022
1313
ms.author: jmprieur
1414
ms.custom: aaddev
1515
#Customer intent: As an application developer, I want to know how to write a web app that signs in users by using the Microsoft identity platform.
@@ -89,7 +89,7 @@ By default, the sample uses:
8989
1. Enter a key description.
9090
1. Select the key duration **In 1 year**.
9191
1. Select **Add**.
92-
1. When the key value appears, copy it for later. This value will not be displayed again or be retrievable by any other means.
92+
1. When the key value appears, copy it for later. This value won't be displayed again or be retrievable by any other means.
9393

9494
# [Node.js](#tab/nodejs)
9595

@@ -124,13 +124,70 @@ By default, the sample uses:
124124

125125
## Register an app by using PowerShell
126126

127-
> [!NOTE]
128-
> Currently, Azure AD PowerShell creates applications with only the following supported account types:
129-
>
130-
> - MyOrg (accounts in this organizational directory only)
131-
> - AnyOrg (accounts in any organizational directory)
132-
>
133-
> You can create an application that signs in users with their personal Microsoft accounts (for example, Skype, Xbox, or Outlook.com). First, create a multitenant application. Supported account types are accounts in any organizational directory. Then, change the [`accessTokenAcceptedVersion`](./reference-app-manifest.md#accesstokenacceptedversion-attribute) property to **2** and the [`signInAudience`](./reference-app-manifest.md#signinaudience-attribute) property to `AzureADandPersonalMicrosoftAccount` in the [application manifest](./reference-app-manifest.md) from the Azure portal. For more information, see [step 1.3](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/1-WebApp-OIDC/1-3-AnyOrgOrPersonal#step-1-register-the-sample-with-your-azure-ad-tenant) in the ASP.NET Core tutorial. You can generalize this step to web apps in any language.
127+
You can also register an application with Microsoft Graph PowerShell, using the [New-MgApplication](/powershell/module/microsoft.graph.applications/new-mgapplication).
128+
129+
Here's an idea of the code. For a fully functioning code, see [this sample](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/master/1-WebApp-OIDC/1-3-AnyOrgOrPersonal/AppCreationScripts/Configure.ps1)
130+
131+
```PowerShell
132+
# Connect to the Microsoft Graph API, non-interactive is not supported for the moment (Oct 2021)
133+
Write-Host "Connecting to Microsoft Graph"
134+
if ($tenantId -eq "") {
135+
Connect-MgGraph -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
136+
}
137+
else {
138+
Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
139+
}
140+
141+
$context = Get-MgContext
142+
$tenantId = $context.TenantId
143+
144+
# Get the user running the script
145+
$currentUserPrincipalName = $context.Account
146+
$user = Get-MgUser -Filter "UserPrincipalName eq '$($context.Account)'"
147+
148+
# get the tenant we signed in to
149+
$Tenant = Get-MgOrganization
150+
$tenantName = $Tenant.DisplayName
151+
152+
$verifiedDomain = $Tenant.VerifiedDomains | where {$_.Isdefault -eq $true}
153+
$verifiedDomainName = $verifiedDomain.Name
154+
$tenantId = $Tenant.Id
155+
156+
Write-Host ("Connected to Tenant {0} ({1}) as account '{2}'. Domain is '{3}'" -f $Tenant.DisplayName, $Tenant.Id, $currentUserPrincipalName, $verifiedDomainName)
157+
158+
# Create the webApp AAD application
159+
Write-Host "Creating the AAD application (WebApp)"
160+
# create the application
161+
$webAppAadApplication = New-MgApplication -DisplayName "WebApp" `
162+
-Web `
163+
@{ `
164+
RedirectUris = "https://localhost:44321/", "https://localhost:44321/signin-oidc"; `
165+
HomePageUrl = "https://localhost:44321/"; `
166+
LogoutUrl = "https://localhost:44321/signout-oidc"; `
167+
} `
168+
-SignInAudience AzureADandPersonalMicrosoftAccount `
169+
#end of command
170+
171+
$currentAppId = $webAppAadApplication.AppId
172+
$currentAppObjectId = $webAppAadApplication.Id
173+
174+
$tenantName = (Get-MgApplication -ApplicationId $currentAppObjectId).PublisherDomain
175+
#Update-MgApplication -ApplicationId $currentAppObjectId -IdentifierUris @("https://$tenantName/WebApp")
176+
177+
# create the service principal of the newly created application
178+
$webAppServicePrincipal = New-MgServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
179+
180+
# add the user running the script as an app owner if needed
181+
$owner = Get-MgApplicationOwner -ApplicationId $currentAppObjectId
182+
if ($owner -eq $null)
183+
{
184+
New-MgApplicationOwnerByRef -ApplicationId $currentAppObjectId -BodyParameter = @{"@odata.id" = "htps://graph.microsoft.com/v1.0/directoryObjects/$user.ObjectId"}
185+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
186+
}
187+
Write-Host "Done creating the webApp application (WebApp)"
188+
```
189+
190+
134191

135192
## Next steps
136193

0 commit comments

Comments
 (0)