1+ # Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
2+ # The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
3+ # described in $permissionType
4+ Function AddResourcePermission ($requiredAccess , `
5+ $exposedPermissions , [string ]$requiredAccesses , [string ]$permissionType )
6+ {
7+ foreach ($permission in $requiredAccesses.Trim ().Split(" |" ))
8+ {
9+ foreach ($exposedPermission in $exposedPermissions )
10+ {
11+ if ($exposedPermission.Value -eq $permission )
12+ {
13+ $resourceAccess = New-Object Microsoft.Open.AzureAD.Model.ResourceAccess
14+ $resourceAccess.Type = $permissionType # Scope = Delegated permissions | Role = Application permissions
15+ $resourceAccess.Id = $exposedPermission.Id # Read directory data
16+ $requiredAccess.ResourceAccess.Add ($resourceAccess )
17+ }
18+ }
19+ }
20+ }
21+
22+ #
23+ # Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
24+ # See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
25+ Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal )
26+ {
27+ # If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
28+ if ($servicePrincipal )
29+ {
30+ $sp = $servicePrincipal
31+ }
32+ else
33+ {
34+ $sp = Get-AzureADServicePrincipal - Filter " DisplayName eq '$applicationDisplayName '"
35+ }
36+ $appid = $sp.AppId
37+ $requiredAccess = New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess
38+ $requiredAccess.ResourceAppId = $appid
39+ $requiredAccess.ResourceAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.ResourceAccess ]
40+
41+ # $sp.Oauth2Permissions | Select Id,AdminConsentDisplayName,Value: To see the list of all the Delegated permissions for the application:
42+ if ($requiredDelegatedPermissions )
43+ {
44+ AddResourcePermission $requiredAccess - exposedPermissions $sp.Oauth2Permissions - requiredAccesses $requiredDelegatedPermissions - permissionType " Scope"
45+ }
46+
47+ # $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
48+ if ($requiredDelegatedPermissions )
49+ {
50+ AddResourcePermission $requiredAccess - exposedPermissions $sp.AppRoles - requiredAccesses $requiredApplicationPermissions - permissionType " Role"
51+ }
52+ return $requiredAccess
53+ }
54+
55+ # Replace the value of an appsettings of a given key in an XML App.Config file.
56+ Function ReplaceSetting ([string ] $configFilePath , [string ] $key , [string ] $newValue )
57+ {
58+ [xml ] $content = Get-Content $configFilePath
59+ $appSettings = $content.configuration.appSettings ;
60+ $keyValuePair = $appSettings.SelectSingleNode (" descendant::add[@key='$key ']" )
61+ if ($keyValuePair )
62+ {
63+ $keyValuePair.value = $newValue ;
64+ }
65+ else
66+ {
67+ Throw " Key '$key ' not found in file '$configFilePath '"
68+ }
69+ $content.save ($configFilePath )
70+ }
71+
72+ Function ConfigureApplications
73+ {
74+ <# . Description
75+ This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
76+ configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
77+ so that they are consistent with the Applications parameters
78+ #>
79+ [CmdletBinding ()]
80+ param (
81+ [PSCredential ] $Credential ,
82+ [Parameter (HelpMessage = ' Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps' )]
83+ [string ] $tenantId
84+ )
85+
86+ process
87+ {
88+ # $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
89+ # into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
90+
91+ # Login to Azure PowerShell (interactive if credentials are not already provided:
92+ # you'll need to sign-in with creds enabling your to create apps in the tenant)
93+ if (! $Credential -and $TenantId )
94+ {
95+ $creds = Connect-AzureAD - TenantId $tenantId
96+ }
97+ else
98+ {
99+ if (! $TenantId )
100+ {
101+ $creds = Connect-AzureAD - Credential $Credential
102+ }
103+ else
104+ {
105+ $creds = Connect-AzureAD - TenantId $tenantId - Credential $Credential
106+ }
107+ }
108+
109+ if (! $tenantId )
110+ {
111+ $tenantId = $creds.Tenant.Id
112+ }
113+ $tenant = Get-AzureADTenantDetail
114+ $tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
115+
116+ # Create the service AAD application
117+ Write-Host " Creating the AAD appplication (TodoListService-ManualJwt)"
118+ $serviceAadApplication = New-AzureADApplication - DisplayName " TodoListService-ManualJwt" `
119+ - HomePage " https://localhost:44324" `
120+ - IdentifierUris " https://$tenantName /TodoListService-ManualJwt" `
121+ - PublicClient $False
122+ $serviceServicePrincipal = New-AzureADServicePrincipal - AppId $serviceAadApplication.AppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
123+ Write-Host " Done."
124+
125+ # Create the client AAD application
126+ Write-Host " Creating the AAD appplication (TodoListClient-ManualJwt)"
127+ $clientAadApplication = New-AzureADApplication - DisplayName " TodoListClient-ManualJwt" `
128+ - ReplyUrls " https://TodoListClient-ManualJwt" `
129+ - PublicClient $True
130+ $clientServicePrincipal = New-AzureADServicePrincipal - AppId $clientAadApplication.AppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
131+ Write-Host " Done."
132+
133+ # Add Required Resources Access (from 'client' to 'service')
134+ Write-Host " Getting access from 'client' to 'service'"
135+ $requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess ]
136+ $requiredPermissions = GetRequiredPermissions - applicationDisplayName " TodoListService-ManualJwt" `
137+ - requiredDelegatedPermissions " user_impersonation" ;
138+ $requiredResourcesAccess.Add ($requiredPermissions )
139+ Set-AzureADApplication - ObjectId $clientAadApplication.ObjectId - RequiredResourceAccess $requiredResourcesAccess
140+ Write-Host " Granted."
141+
142+ # Update config file for 'service'
143+ $configFile = $pwd.Path + " \..\TodoListService-ManualJwt\Web.Config"
144+ Write-Host " Updating the sample code ($configFile )"
145+ ReplaceSetting - configFilePath $configFile - key " ida:Tenant" - newValue $tenantName
146+ ReplaceSetting - configFilePath $configFile - key " ida:Audience" - newValue $serviceAadApplication.IdentifierUris
147+
148+ # Update config file for 'client'
149+ $configFile = $pwd.Path + " \..\TodoListClient\App.Config"
150+ Write-Host " Updating the sample code ($configFile )"
151+ ReplaceSetting - configFilePath $configFile - key " ida:Tenant" - newValue $tenantName
152+ ReplaceSetting - configFilePath $configFile - key " ida:ClientId" - newValue $clientAadApplication.AppId
153+ ReplaceSetting - configFilePath $configFile - key " ida:RedirectUri" - newValue $clientAadApplication.ReplyUrls
154+ ReplaceSetting - configFilePath $configFile - key " todo:TodoListResourceId" - newValue $serviceAadApplication.IdentifierUris
155+ ReplaceSetting - configFilePath $configFile - key " todo:TodoListBaseAddress" - newValue $serviceAadApplication.HomePage
156+ }
157+ }
158+
159+ # Run interactively (will ask you for the tenant ID)
160+ ConfigureApplications - Credential $Credential - tenantId $TenantId
0 commit comments