Skip to content

Commit 6686ab6

Browse files
author
Tiago Brenck
authored
Merge pull request #100 from Azure-Samples/fixAppRegistrationScripts
Fixing AppCreationScripts
2 parents 9fc46d0 + 17970d3 commit 6686ab6

File tree

9 files changed

+237
-51
lines changed

9 files changed

+237
-51
lines changed

1. Desktop app calls Web API/AppCreationScripts/AppCreationScripts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Registering the Azure Active Directory applications and updating the configuration files for this sample using PowerShell scripts
1+
# Registering the sample apps with Microsoft Identity Platform and updating the configuration files using PowerShell scripts
22

33
## Overview
44

@@ -9,7 +9,7 @@
99
```PowerShell
1010
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1111
```
12-
1. Run the script to create your Azure AD application and configure the code of the sample application accordinly. (Other ways of running the scripts are described below)
12+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. (Other ways of running the scripts are described below)
1313
```PowerShell
1414
.\AppCreationScripts\Configure.ps1
1515
```

1. Desktop app calls Web API/AppCreationScripts/Cleanup.ps1

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{
@@ -50,22 +50,37 @@ This function removes the Azure AD applications for the sample. These applicatio
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'service' (TodoListService (active-directory-dotnet-native-aspnetcore-v2)) if needed"
53-
$apps=Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

5560
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed TodoListService (active-directory-dotnet-native-aspnetcore-v2)."
63+
Write-Host "Removed TodoListService (active-directory-dotnet-native-aspnetcore-v2).."
64+
}
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
68+
Write-Host "Removing 'client' (TodoListClient (active-directory-dotnet-native-aspnetcore-v2)) if needed"
69+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
70+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'"
71+
if ($apps)
72+
{
73+
Remove-AzureADApplication -ObjectId $apps.ObjectId
5974
}
60-
61-
Write-Host "Removing 'client' (TodoListClient (active-directory-dotnet-native-aspnetcore-v2)) if needed"
62-
$apps=Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'"
6375

6476
foreach ($app in $apps)
6577
{
6678
Remove-AzureADApplication -ObjectId $app.ObjectId
67-
Write-Host "Removed TodoListClient (active-directory-dotnet-native-aspnetcore-v2)."
79+
Write-Host "Removed TodoListClient (active-directory-dotnet-native-aspnetcore-v2).."
6880
}
81+
# also remove service principals of this app
82+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
83+
6984
}
7085

71-
Cleanup -Credential $Credential -tenantId $TenantId
86+
Cleanup -Credential $Credential -tenantId $TenantId

1. Desktop app calls Web API/AppCreationScripts/Configure.ps1

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Function AddResourcePermission($requiredAccess, `
3939
}
4040

4141
#
42-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
42+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
4343
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
4444
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
4545
{
@@ -125,17 +125,56 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
125125

126126
Set-Content -Path $configFilePath -Value $lines -Force
127127
}
128+
<#.Description
129+
This function creates a new Azure AD scope (OAuth2Permission) with default and provided values
130+
#>
131+
Function CreateScope( [string] $value, [string] $userConsentDisplayName, [string] $userConsentDescription, [string] $adminConsentDisplayName, [string] $adminConsentDescription)
132+
{
133+
$scope = New-Object Microsoft.Open.AzureAD.Model.OAuth2Permission
134+
$scope.Id = New-Guid
135+
$scope.Value = $value
136+
$scope.UserConsentDisplayName = $userConsentDisplayName
137+
$scope.UserConsentDescription = $userConsentDescription
138+
$scope.AdminConsentDisplayName = $adminConsentDisplayName
139+
$scope.AdminConsentDescription = $adminConsentDescription
140+
$scope.IsEnabled = $true
141+
$scope.Type = "User"
142+
return $scope
143+
}
144+
145+
<#.Description
146+
This function creates a new Azure AD AppRole with default and provided values
147+
#>
148+
Function CreateAppRole([string] $types, [string] $name, [string] $description)
149+
{
150+
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
151+
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
152+
$typesArr = $types.Split(',')
153+
foreach($type in $typesArr)
154+
{
155+
$appRole.AllowedMemberTypes.Add($type);
156+
}
157+
$appRole.DisplayName = $name
158+
$appRole.Id = New-Guid
159+
$appRole.IsEnabled = $true
160+
$appRole.Description = $description
161+
$appRole.Value = $name;
162+
return $appRole
163+
}
128164

129165
Set-Content -Value "<html><body><table>" -Path createdApps.html
130166
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
131167

168+
$ErrorActionPreference = "Stop"
169+
132170
Function ConfigureApplications
133171
{
134172
<#.Description
135173
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
136174
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
137175
so that they are consistent with the Applications parameters
138176
#>
177+
$commonendpoint = "common"
139178

140179
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
141180
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -166,54 +205,90 @@ Function ConfigureApplications
166205
$tenant = Get-AzureADTenantDetail
167206
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
168207

169-
# Get the user running the script
208+
# Get the user running the script to add the user as the app owner
170209
$user = Get-AzureADUser -ObjectId $creds.Account.Id
171210

172211
# Create the service AAD application
173212
Write-Host "Creating the AAD application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
213+
# create the application
174214
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
175215
-HomePage "https://localhost:44351/" `
176216
-AvailableToOtherTenants $True `
177217
-PublicClient $False
178218
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
179219
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -IdentifierUris $serviceIdentifierUri
180220

221+
# create the service principal of the newly created application
181222
$currentAppId = $serviceAadApplication.AppId
182223
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
183224

184225
# add the user running the script as an app owner if needed
185226
$owner = Get-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId
186227
if ($owner -eq $null)
187228
{
188-
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
189-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
229+
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
230+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
190231
}
191232

233+
# rename the user_impersonation scope if it exists to match the readme steps or add a new scope
234+
$scopes = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.OAuth2Permission]
235+
236+
if ($scopes.Count -ge 0)
237+
{
238+
# add all existing scopes first
239+
$serviceAadApplication.Oauth2Permissions | foreach-object { $scopes.Add($_) }
240+
241+
$scope = $serviceAadApplication.Oauth2Permissions | Where-Object { $_.Value -eq "User_impersonation" }
242+
243+
if ($scope -ne $null)
244+
{
245+
$scope.Value = "access_as_user"
246+
}
247+
else
248+
{
249+
# Add scope
250+
$scope = CreateScope -value "access_as_user" `
251+
-userConsentDisplayName "Access TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
252+
-userConsentDescription "Allow the application to access TodoListService (active-directory-dotnet-native-aspnetcore-v2) on your behalf." `
253+
-adminConsentDisplayName "Access TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
254+
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of the signed-in user."
255+
256+
$scopes.Add($scope)
257+
}
258+
}
259+
260+
# add/update scopes
261+
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -OAuth2Permission $scopes
262+
192263
Write-Host "Done creating the service application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
193264

194265
# URL of the AAD application in the Azure portal
195266
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
196267
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
197268
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService (active-directory-dotnet-native-aspnetcore-v2)</a></td></tr>" -Path createdApps.html
198269

270+
199271
# Create the client AAD application
200272
Write-Host "Creating the AAD application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
273+
# create the application
201274
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient (active-directory-dotnet-native-aspnetcore-v2)" `
202-
-ReplyUrls "urn:ietf:wg:oauth:2.0:oob" `
275+
-ReplyUrls "https://login.microsoftonline.com/common/oauth2/nativeclient" `
203276
-AvailableToOtherTenants $True `
204277
-PublicClient $True
205278

279+
# create the service principal of the newly created application
206280
$currentAppId = $clientAadApplication.AppId
207281
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
208282

209283
# add the user running the script as an app owner if needed
210284
$owner = Get-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId
211285
if ($owner -eq $null)
212286
{
213-
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
214-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
287+
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
288+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
215289
}
216290

291+
217292
Write-Host "Done creating the client application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
218293

219294
# URL of the AAD application in the Azure portal
@@ -226,7 +301,7 @@ Function ConfigureApplications
226301
# Add Required Resources Access (from 'client' to 'service')
227302
Write-Host "Getting access from 'client' to 'service'"
228303
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
229-
-requiredDelegatedPermissions "user_impersonation" `
304+
-requiredDelegatedPermissions "access_as_user" `
230305

231306
$requiredResourcesAccess.Add($requiredPermissions)
232307

@@ -245,7 +320,7 @@ Function ConfigureApplications
245320
Write-Host "Updating the sample code ($configFile)"
246321
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue $tenantName
247322
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $clientAadApplication.AppId
248-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListScope" -newValue ("api://"+$serviceAadApplication.AppId+"/user_impersonation")
323+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListScope" -newValue ("api://"+$serviceAadApplication.AppId+"/access_as_user")
249324
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
250325
Write-Host ""
251326
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
@@ -257,14 +332,17 @@ Function ConfigureApplications
257332
Write-Host "- For 'client'"
258333
Write-Host " - Navigate to '$clientPortalUrl'"
259334
Write-Host " - Navigate to the Manifest page and change 'signInAudience' to 'AzureADandPersonalMicrosoftAccount'." -ForegroundColor Red
335+
260336
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
337+
261338
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
262339
}
263340

264341
# Pre-requisites
265342
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
266343
Install-Module "AzureAD" -Scope CurrentUser
267-
}
344+
}
345+
268346
Import-Module AzureAD
269347

270348
# Run interactively (will ask you for the tenant ID)

1. Desktop app calls Web API/AppCreationScripts/sample.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
"Id": "client",
3131
"Name": "TodoListClient (active-directory-dotnet-native-aspnetcore-v2)",
3232
"Kind": "Desktop",
33+
"ReplyUrls": "https://login.microsoftonline.com/common/oauth2/nativeclient",
3334
"RequiredResourcesAccess": [
3435
{
3536
"Resource": "service",
36-
"DelegatedPermissions": [ "user_impersonation" ]
37+
"DelegatedPermissions": [ "access_as_user" ]
3738
}
3839
],
3940
"ManualSteps": [

3.-Web-api-call-Microsoft-graph-for-personal-accounts/AppCreationScripts/AppCreationScripts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Registering the Azure Active Directory applications and updating the configuration files for this sample using PowerShell scripts
1+
# Registering the sample apps with Microsoft Identity Platform and updating the configuration files using PowerShell scripts
22

33
## Overview
44

@@ -9,7 +9,7 @@
99
```PowerShell
1010
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1111
```
12-
1. Run the script to create your Azure AD application and configure the code of the sample application accordinly. (Other ways of running the scripts are described below)
12+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. (Other ways of running the scripts are described below)
1313
```PowerShell
1414
.\AppCreationScripts\Configure.ps1
1515
```

3.-Web-api-call-Microsoft-graph-for-personal-accounts/AppCreationScripts/Cleanup.ps1

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ param(
55
[string] $tenantId
66
)
77

8-
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{
@@ -44,29 +44,43 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'service' (TodoListService (active-directory-dotnet-native-aspnetcore-v2)) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

55-
if ($app)
60+
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed."
63+
Write-Host "Removed TodoListService (active-directory-dotnet-native-aspnetcore-v2).."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
6168
Write-Host "Removing 'client' (TodoListClient (active-directory-dotnet-native-aspnetcore-v2)) if needed"
62-
$app=Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'"
69+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
70+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'"
71+
if ($apps)
72+
{
73+
Remove-AzureADApplication -ObjectId $apps.ObjectId
74+
}
6375

64-
if ($app)
76+
foreach ($app in $apps)
6577
{
6678
Remove-AzureADApplication -ObjectId $app.ObjectId
67-
Write-Host "Removed."
79+
Write-Host "Removed TodoListClient (active-directory-dotnet-native-aspnetcore-v2).."
6880
}
69-
81+
# also remove service principals of this app
82+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
83+
7084
}
7185

72-
Cleanup -Credential $Credential -tenantId $TenantId
86+
Cleanup -Credential $Credential -tenantId $TenantId

0 commit comments

Comments
 (0)