@@ -17,87 +17,6 @@ param(
1717 There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
1818#>
1919
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-
10120Function UpdateLine ([string ] $line , [string ] $value )
10221{
10322 $index = $line.IndexOf (' =' )
@@ -137,16 +56,13 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
13756Set-Content - Value " <html><body><table>" - Path createdApps.html
13857Add-Content - Value " <thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" - Path createdApps.html
13958
140- $ErrorActionPreference = " Stop"
141-
14259Function ConfigureApplications
14360{
14461<# . Description
14562 This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
14663 configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
14764 so that they are consistent with the Applications parameters
14865#>
149- $commonendpoint = " common"
15066
15167 # $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
15268 # 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
17793 $tenant = Get-AzureADTenantDetail
17894 $tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
17995
180- # Get the user running the script to add the user as the app owner
96+ # Get the user running the script
18197 $user = Get-AzureADUser - ObjectId $creds.Account.Id
18298
18399 # Create the webApp AAD application
184100 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
191101 $webAppAadApplication = New-AzureADApplication - DisplayName " WebApp" `
192102 - HomePage " https://localhost:44321/" `
193103 - LogoutUrl " https://localhost:44321/signout-oidc" `
194104 - ReplyUrls " https://localhost:44321/" , " https://localhost:44321/signin-oidc" `
195105 - IdentifierUris " https://$tenantName /WebApp" `
196106 - AvailableToOtherTenants $True `
197- - PasswordCredentials $key `
198107 - Oauth2AllowImplicitFlow $true `
199108 - PublicClient $False
200109
201- # create the service principal of the newly created application
202110 $currentAppId = $webAppAadApplication.AppId
203111 $webAppServicePrincipal = New-AzureADServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
204112
205113 # add the user running the script as an app owner if needed
206114 $owner = Get-AzureADApplicationOwner - ObjectId $webAppAadApplication.ObjectId
207115 if ($owner -eq $null )
208116 {
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 ) '"
211119 }
212120
213-
214121 Write-Host " Done creating the webApp application (WebApp)"
215122
216123 # URL of the AAD application in the Azure portal
217124 # Future? $webAppPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
218125 $webAppPortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $webAppAadApplication.AppId + " /objectId/" + $webAppAadApplication.ObjectId + " /isMSAApp/"
219126 Add-Content - Value " <tr><td>webApp</td><td>$currentAppId </td><td><a href='$webAppPortalUrl '>WebApp</a></td></tr>" - Path createdApps.html
220127
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."
233128
234129 # Update config file for 'webApp'
235130 $configFile = $pwd.Path + " \..\appsettings.json"
236131 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 };
238133 UpdateTextFile - configFilePath $configFile - dictionary $dictionary
239-
134+
240135 Add-Content - Value " </tbody></table></body></html>" - Path createdApps.html
241136}
242137
243138# Pre-requisites
244139if ((Get-Module - ListAvailable - Name " AzureAD" ) -eq $null ) {
245140 Install-Module " AzureAD" - Scope CurrentUser
246- }
247-
141+ }
248142Import-Module AzureAD
249143
250144# Run interactively (will ask you for the tenant ID)
0 commit comments