@@ -39,7 +39,7 @@ Function AddResourcePermission($requiredAccess, `
39
39
}
40
40
41
41
#
42
- # Exemple : GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
42
+ # Example : GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
43
43
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
44
44
Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal )
45
45
{
@@ -125,17 +125,56 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
125
125
126
126
Set-Content - Path $configFilePath - Value $lines - Force
127
127
}
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
+ }
128
164
129
165
Set-Content - Value " <html><body><table>" - Path createdApps.html
130
166
Add-Content - Value " <thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" - Path createdApps.html
131
167
168
+ $ErrorActionPreference = " Stop"
169
+
132
170
Function ConfigureApplications
133
171
{
134
172
<# . Description
135
173
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
136
174
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
137
175
so that they are consistent with the Applications parameters
138
176
#>
177
+ $commonendpoint = " common"
139
178
140
179
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
141
180
# 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
166
205
$tenant = Get-AzureADTenantDetail
167
206
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
168
207
169
- # Get the user running the script
208
+ # Get the user running the script to add the user as the app owner
170
209
$user = Get-AzureADUser - ObjectId $creds.Account.Id
171
210
172
211
# Create the service AAD application
173
212
Write-Host " Creating the AAD application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
213
+ # create the application
174
214
$serviceAadApplication = New-AzureADApplication - DisplayName " TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
175
215
- HomePage " https://localhost:44351/" `
176
216
- AvailableToOtherTenants $True `
177
217
- PublicClient $False
178
218
$serviceIdentifierUri = ' api://' + $serviceAadApplication.AppId
179
219
Set-AzureADApplication - ObjectId $serviceAadApplication.ObjectId - IdentifierUris $serviceIdentifierUri
180
220
221
+ # create the service principal of the newly created application
181
222
$currentAppId = $serviceAadApplication.AppId
182
223
$serviceServicePrincipal = New-AzureADServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
183
224
184
225
# add the user running the script as an app owner if needed
185
226
$owner = Get-AzureADApplicationOwner - ObjectId $serviceAadApplication.ObjectId
186
227
if ($owner -eq $null )
187
228
{
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 ) '"
190
231
}
191
232
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
+
192
263
Write-Host " Done creating the service application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
193
264
194
265
# URL of the AAD application in the Azure portal
195
266
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
196
267
$servicePortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $serviceAadApplication.AppId + " /objectId/" + $serviceAadApplication.ObjectId + " /isMSAApp/"
197
268
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
198
269
270
+
199
271
# Create the client AAD application
200
272
Write-Host " Creating the AAD application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
273
+ # create the application
201
274
$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 " `
203
276
- AvailableToOtherTenants $True `
204
277
- PublicClient $True
205
278
279
+ # create the service principal of the newly created application
206
280
$currentAppId = $clientAadApplication.AppId
207
281
$clientServicePrincipal = New-AzureADServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
208
282
209
283
# add the user running the script as an app owner if needed
210
284
$owner = Get-AzureADApplicationOwner - ObjectId $clientAadApplication.ObjectId
211
285
if ($owner -eq $null )
212
286
{
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 ) '"
215
289
}
216
290
291
+
217
292
Write-Host " Done creating the client application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
218
293
219
294
# URL of the AAD application in the Azure portal
@@ -226,7 +301,7 @@ Function ConfigureApplications
226
301
# Add Required Resources Access (from 'client' to 'service')
227
302
Write-Host " Getting access from 'client' to 'service'"
228
303
$requiredPermissions = GetRequiredPermissions - applicationDisplayName " TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
229
- - requiredDelegatedPermissions " user_impersonation " `
304
+ - requiredDelegatedPermissions " access_as_user " `
230
305
231
306
$requiredResourcesAccess.Add ($requiredPermissions )
232
307
@@ -245,7 +320,7 @@ Function ConfigureApplications
245
320
Write-Host " Updating the sample code ($configFile )"
246
321
ReplaceSetting - configFilePath $configFile - key " ida:Tenant" - newValue $tenantName
247
322
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 " )
249
324
ReplaceSetting - configFilePath $configFile - key " todo:TodoListBaseAddress" - newValue $serviceAadApplication.HomePage
250
325
Write-Host " "
251
326
Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
@@ -257,14 +332,17 @@ Function ConfigureApplications
257
332
Write-Host " - For 'client'"
258
333
Write-Host " - Navigate to '$clientPortalUrl '"
259
334
Write-Host " - Navigate to the Manifest page and change 'signInAudience' to 'AzureADandPersonalMicrosoftAccount'." - ForegroundColor Red
335
+
260
336
Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
337
+
261
338
Add-Content - Value " </tbody></table></body></html>" - Path createdApps.html
262
339
}
263
340
264
341
# Pre-requisites
265
342
if ((Get-Module - ListAvailable - Name " AzureAD" ) -eq $null ) {
266
343
Install-Module " AzureAD" - Scope CurrentUser
267
- }
344
+ }
345
+
268
346
Import-Module AzureAD
269
347
270
348
# Run interactively (will ask you for the tenant ID)
0 commit comments