@@ -17,32 +17,6 @@ param(
17
17
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
18
18
#>
19
19
20
- # Create a password that can be used as an application key
21
- Function ComputePassword
22
- {
23
- $aesManaged = New-Object " System.Security.Cryptography.AesManaged"
24
- $aesManaged.Mode = [System.Security.Cryptography.CipherMode ]::CBC
25
- $aesManaged.Padding = [System.Security.Cryptography.PaddingMode ]::Zeros
26
- $aesManaged.BlockSize = 128
27
- $aesManaged.KeySize = 256
28
- $aesManaged.GenerateKey ()
29
- return [System.Convert ]::ToBase64String($aesManaged.Key )
30
- }
31
-
32
- # Create an application key
33
- # See https://www.sabin.io/blog/adding-an-azure-active-directory-application-and-key-using-powershell/
34
- Function CreateAppKey ([DateTime ] $fromDate , [double ] $durationInYears , [string ]$pw )
35
- {
36
- $endDate = $fromDate.AddYears ($durationInYears )
37
- $keyId = (New-Guid ).ToString();
38
- $key = New-Object Microsoft.Open.AzureAD.Model.PasswordCredential
39
- $key.StartDate = $fromDate
40
- $key.EndDate = $endDate
41
- $key.Value = $pw
42
- $key.KeyId = $keyId
43
- return $key
44
- }
45
-
46
20
# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
47
21
# The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
48
22
# described in $permissionType
@@ -65,7 +39,7 @@ Function AddResourcePermission($requiredAccess, `
65
39
}
66
40
67
41
#
68
- # Exemple : GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
42
+ # Example : GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
69
43
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
70
44
Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal )
71
45
{
@@ -134,49 +108,18 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
134
108
Set-Content - Path $configFilePath - Value $lines - Force
135
109
}
136
110
137
- Function ReplaceInLine ([string ] $line , [string ] $key , [string ] $value )
138
- {
139
- $index = $line.IndexOf ($key )
140
- if ($index -ige 0 )
141
- {
142
- $index2 = $index + $key.Length
143
- $line = $line.Substring (0 , $index ) + $value + $line.Substring ($index2 )
144
- }
145
- return $line
146
- }
147
-
148
- Function ReplaceInTextFile ([string ] $configFilePath , [System.Collections.HashTable ] $dictionary )
149
- {
150
- $lines = Get-Content $configFilePath
151
- $index = 0
152
- while ($index -lt $lines.Length )
153
- {
154
- $line = $lines [$index ]
155
- foreach ($key in $dictionary.Keys )
156
- {
157
- if ($line.Contains ($key ))
158
- {
159
- $lines [$index ] = ReplaceInLine $line $key $dictionary [$key ]
160
- }
161
- }
162
- $index ++
163
- }
164
-
165
- Set-Content - Path $configFilePath - Value $lines - Force
166
- }
167
-
168
-
169
111
Set-Content - Value " <html><body><table>" - Path createdApps.html
170
112
Add-Content - Value " <thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" - Path createdApps.html
171
113
114
+ $ErrorActionPreference = " Stop"
115
+
172
116
Function ConfigureApplications
173
117
{
174
118
<# . Description
175
119
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
176
120
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
177
121
so that they are consistent with the Applications parameters
178
122
#>
179
-
180
123
$commonendpoint = " common"
181
124
182
125
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
@@ -208,18 +151,18 @@ Function ConfigureApplications
208
151
$tenant = Get-AzureADTenantDetail
209
152
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
210
153
211
- # Get the user running the script
154
+ # Get the user running the script to add the user as the app owner
212
155
$user = Get-AzureADUser - ObjectId $creds.Account.Id
213
156
214
157
# Create the client AAD application
215
- Write-Host " Creating the AAD application (python-daemon-console )"
216
-
217
- $clientAadApplication = New-AzureADApplication - DisplayName " python-device-flow " `
218
- - ReplyUrls " https://device " `
219
- - IdentifierUris " https:// $tenantName /python-device-flow " `
220
- - PasswordCredentials $key `
221
- - PublicClient $False
222
-
158
+ Write-Host " Creating the AAD application (python-devicecode )"
159
+ # create the application
160
+ $clientAadApplication = New-AzureADApplication - DisplayName " python-devicecode " `
161
+ - ReplyUrls " https://login.microsoftonline.com/common/oauth2/nativeclient " `
162
+ - AvailableToOtherTenants $True `
163
+ - PublicClient $True
164
+
165
+ # create the service principal of the newly created application
223
166
$currentAppId = $clientAadApplication.AppId
224
167
$clientServicePrincipal = New-AzureADServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
225
168
@@ -232,19 +175,19 @@ Function ConfigureApplications
232
175
}
233
176
234
177
235
- Write-Host " Done creating the client application (python-device-flow )"
178
+ Write-Host " Done creating the client application (python-devicecode )"
236
179
237
180
# URL of the AAD application in the Azure portal
238
181
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
239
182
$clientPortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $clientAadApplication.AppId + " /objectId/" + $clientAadApplication.ObjectId + " /isMSAApp/"
240
- Add-Content - Value " <tr><td>client</td><td>$currentAppId </td><td><a href='$clientPortalUrl '>python-device-flow </a></td></tr>" - Path createdApps.html
183
+ Add-Content - Value " <tr><td>client</td><td>$currentAppId </td><td><a href='$clientPortalUrl '>python-devicecode </a></td></tr>" - Path createdApps.html
241
184
242
185
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess ]
243
186
244
187
# Add Required Resources Access (from 'client' to 'Microsoft Graph')
245
188
Write-Host " Getting access from 'client' to 'Microsoft Graph'"
246
189
$requiredPermissions = GetRequiredPermissions - applicationDisplayName " Microsoft Graph" `
247
- - requiredApplicationPermissions " User.Read.All " `
190
+ - requiredDelegatedPermissions " User.Read" `
248
191
249
192
$requiredResourcesAccess.Add ($requiredPermissions )
250
193
@@ -255,31 +198,18 @@ Function ConfigureApplications
255
198
# Update config file for 'client'
256
199
$configFile = $pwd.Path + " \..\parameters.json"
257
200
Write-Host " Updating the sample code ($configFile )"
258
- $dictionary = @ { " client_id" = $clientAadApplication.AppId ; };
201
+ $dictionary = @ { " client_id" = $clientAadApplication.AppId };
259
202
UpdateTextFile - configFilePath $configFile - dictionary $dictionary
260
-
261
- # Update config file for 'client'
262
- $configFile = $pwd.Path + " \..\parameters.json"
263
- Write-Host " Updating the sample code ($configFile )"
264
- $dictionary = @ { " organizations" = $tenantName };
265
- ReplaceInTextFile - configFilePath $configFile - dictionary $dictionary
266
- Write-Host " "
267
- Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
268
- Write-Host " IMPORTANT: Please follow the instructions below to complete a few manual step(s) in the Azure portal" :
269
- Write-Host " - For 'client'"
270
- Write-Host " - Navigate to '$clientPortalUrl '"
271
- Write-Host " - Navigate to the API permissions page and click on 'Grant admin consent for {tenant}'" - ForegroundColor Red
272
-
273
- Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
274
-
203
+
275
204
Add-Content - Value " </tbody></table></body></html>" - Path createdApps.html
276
205
}
277
206
278
207
# Pre-requisites
279
208
if ((Get-Module - ListAvailable - Name " AzureAD" ) -eq $null ) {
280
209
Install-Module " AzureAD" - Scope CurrentUser
281
- }
210
+ }
211
+
282
212
Import-Module AzureAD
283
213
284
214
# Run interactively (will ask you for the tenant ID)
285
- ConfigureApplications - Credential $Credential - tenantId $TenantId
215
+ ConfigureApplications - Credential $Credential - tenantId $TenantId
0 commit comments