@@ -17,87 +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
- # Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
47
- # The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
48
- # described in $permissionType
49
- Function AddResourcePermission ($requiredAccess , `
50
- $exposedPermissions , [string ]$requiredAccesses , [string ]$permissionType )
51
- {
52
- foreach ($permission in $requiredAccesses.Trim ().Split(" |" ))
53
- {
54
- foreach ($exposedPermission in $exposedPermissions )
55
- {
56
- if ($exposedPermission.Value -eq $permission )
57
- {
58
- $resourceAccess = New-Object Microsoft.Open.AzureAD.Model.ResourceAccess
59
- $resourceAccess.Type = $permissionType # Scope = Delegated permissions | Role = Application permissions
60
- $resourceAccess.Id = $exposedPermission.Id # Read directory data
61
- $requiredAccess.ResourceAccess.Add ($resourceAccess )
62
- }
63
- }
64
- }
65
- }
66
-
67
- #
68
- # Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
69
- # See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
70
- Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal )
71
- {
72
- # If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
73
- if ($servicePrincipal )
74
- {
75
- $sp = $servicePrincipal
76
- }
77
- else
78
- {
79
- $sp = Get-AzureADServicePrincipal - Filter " DisplayName eq '$applicationDisplayName '"
80
- }
81
- $appid = $sp.AppId
82
- $requiredAccess = New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess
83
- $requiredAccess.ResourceAppId = $appid
84
- $requiredAccess.ResourceAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.ResourceAccess ]
85
-
86
- # $sp.Oauth2Permissions | Select Id,AdminConsentDisplayName,Value: To see the list of all the Delegated permissions for the application:
87
- if ($requiredDelegatedPermissions )
88
- {
89
- AddResourcePermission $requiredAccess - exposedPermissions $sp.Oauth2Permissions - requiredAccesses $requiredDelegatedPermissions - permissionType " Scope"
90
- }
91
-
92
- # $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
93
- if ($requiredApplicationPermissions )
94
- {
95
- AddResourcePermission $requiredAccess - exposedPermissions $sp.AppRoles - requiredAccesses $requiredApplicationPermissions - permissionType " Role"
96
- }
97
- return $requiredAccess
98
- }
99
-
100
-
101
20
Function UpdateLine ([string ] $line , [string ] $value )
102
21
{
103
22
$index = $line.IndexOf (' =' )
@@ -137,16 +56,13 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
137
56
Set-Content - Value " <html><body><table>" - Path createdApps.html
138
57
Add-Content - Value " <thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" - Path createdApps.html
139
58
140
- $ErrorActionPreference = " Stop"
141
-
142
59
Function ConfigureApplications
143
60
{
144
61
<# . Description
145
62
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
146
63
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
147
64
so that they are consistent with the Applications parameters
148
65
#>
149
- $commonendpoint = " common"
150
66
151
67
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
152
68
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -177,74 +93,52 @@ Function ConfigureApplications
177
93
$tenant = Get-AzureADTenantDetail
178
94
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
179
95
180
- # Get the user running the script to add the user as the app owner
96
+ # Get the user running the script
181
97
$user = Get-AzureADUser - ObjectId $creds.Account.Id
182
98
183
99
# Create the webApp AAD application
184
100
Write-Host " Creating the AAD application (WebApp)"
185
- # Get a 2 years application key for the webApp Application
186
- $pw = ComputePassword
187
- $fromDate = [DateTime ]::Now;
188
- $key = CreateAppKey - fromDate $fromDate - durationInYears 2 - pw $pw
189
- $webAppAppKey = $pw
190
- # create the application
191
101
$webAppAadApplication = New-AzureADApplication - DisplayName " WebApp" `
192
102
- HomePage " https://localhost:44321/" `
193
103
- LogoutUrl " https://localhost:44321/signout-oidc" `
194
104
- ReplyUrls " https://localhost:44321/" , " https://localhost:44321/signin-oidc" `
195
105
- IdentifierUris " https://$tenantName /WebApp" `
196
106
- AvailableToOtherTenants $True `
197
- - PasswordCredentials $key `
198
107
- Oauth2AllowImplicitFlow $true `
199
108
- PublicClient $False
200
109
201
- # create the service principal of the newly created application
202
110
$currentAppId = $webAppAadApplication.AppId
203
111
$webAppServicePrincipal = New-AzureADServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
204
112
205
113
# add the user running the script as an app owner if needed
206
114
$owner = Get-AzureADApplicationOwner - ObjectId $webAppAadApplication.ObjectId
207
115
if ($owner -eq $null )
208
116
{
209
- Add-AzureADApplicationOwner - ObjectId $webAppAadApplication.ObjectId - RefObjectId $user.ObjectId
210
- Write-Host " '$ ( $user.UserPrincipalName ) ' added as an application owner to app '$ ( $webAppServicePrincipal.DisplayName ) '"
117
+ Add-AzureADApplicationOwner - ObjectId $webAppAadApplication.ObjectId - RefObjectId $user.ObjectId
118
+ Write-Host " '$ ( $user.UserPrincipalName ) ' added as an application owner to app '$ ( $webAppServicePrincipal.DisplayName ) '"
211
119
}
212
120
213
-
214
121
Write-Host " Done creating the webApp application (WebApp)"
215
122
216
123
# URL of the AAD application in the Azure portal
217
124
# Future? $webAppPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
218
125
$webAppPortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $webAppAadApplication.AppId + " /objectId/" + $webAppAadApplication.ObjectId + " /isMSAApp/"
219
126
Add-Content - Value " <tr><td>webApp</td><td>$currentAppId </td><td><a href='$webAppPortalUrl '>WebApp</a></td></tr>" - Path createdApps.html
220
127
221
- $requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess ]
222
-
223
- # Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
224
- Write-Host " Getting access from 'webApp' to 'Microsoft Graph'"
225
- $requiredPermissions = GetRequiredPermissions - applicationDisplayName " Microsoft Graph" `
226
- - requiredDelegatedPermissions " Directory.Read.All" `
227
-
228
- $requiredResourcesAccess.Add ($requiredPermissions )
229
-
230
-
231
- Set-AzureADApplication - ObjectId $webAppAadApplication.ObjectId - RequiredResourceAccess $requiredResourcesAccess
232
- Write-Host " Granted permissions."
233
128
234
129
# Update config file for 'webApp'
235
130
$configFile = $pwd.Path + " \..\appsettings.json"
236
131
Write-Host " Updating the sample code ($configFile )"
237
- $dictionary = @ { " ClientId" = $webAppAadApplication.AppId ;" TenantId" = " organizations" ;" Domain" = $tenantName ; " ClientSecret " = $webAppAppKey };
132
+ $dictionary = @ { " ClientId" = $webAppAadApplication.AppId ;" TenantId" = " organizations" ;" Domain" = $tenantName };
238
133
UpdateTextFile - configFilePath $configFile - dictionary $dictionary
239
-
134
+
240
135
Add-Content - Value " </tbody></table></body></html>" - Path createdApps.html
241
136
}
242
137
243
138
# Pre-requisites
244
139
if ((Get-Module - ListAvailable - Name " AzureAD" ) -eq $null ) {
245
140
Install-Module " AzureAD" - Scope CurrentUser
246
- }
247
-
141
+ }
248
142
Import-Module AzureAD
249
143
250
144
# Run interactively (will ask you for the tenant ID)
0 commit comments