Skip to content

Commit eb3df04

Browse files
author
Tiago Brenck
authored
Merge pull request #42 from Azure-Samples/tibre/cleanup
Cleanup and README update
2 parents 96fb3b6 + bf439a0 commit eb3df04

File tree

17 files changed

+289
-195
lines changed

17 files changed

+289
-195
lines changed

AppCreationScripts/AppCreationScripts.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Registering the Azure Active Directory applications and updating the configuration files for this sample using PowerShell scripts
1+
# Registering the sample apps with Microsoft identity platform and updating the configuration files using PowerShell scripts
22

33
## Overview
44

@@ -9,9 +9,10 @@
99
```PowerShell
1010
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1111
```
12-
1. Run the script to create your Azure AD application and configure the code of the sample application accordinly. (Other ways of running the scripts are described below)
12+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. (Other ways of running the scripts are described below)
1313
```PowerShell
14-
.\AppCreationScripts\Configure.ps1
14+
cd .\AppCreationScripts\
15+
.\Configure.ps1
1516
```
1617
1. Open the Visual Studio solution and click start
1718

@@ -26,6 +27,7 @@ The following paragraphs:
2627
- [Passing credentials](#option-2-non-interactive) to create the app in your home tenant
2728
- [Interactively in a specific tenant](#option-3-interactive-but-create-apps-in-a-specified-tenant)
2829
- [Passing credentials in a specific tenant](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
30+
- [Passing environment name, for Sovereign clouds](#running-the-script-on-azure-sovereign-clouds)
2931

3032
## Goal of the scripts
3133

@@ -49,7 +51,7 @@ These scripts are:
4951

5052
The `Configure.ps1` will stop if it tries to create an Azure AD application which already exists in the tenant. For this, if you are using the script to try/test the sample, or in DevOps scenarios, you might want to run `Cleanup.ps1` just before `Configure.ps1`. This is what is shown in the steps below.
5153

52-
## How to use the app creation scripts ?
54+
## How to use the app creation scripts?
5355

5456
### Pre-requisites
5557

@@ -107,7 +109,7 @@ Note that the script will choose the tenant in which to create the applications,
107109
108110
#### Option 2 (non-interactive)
109111
110-
When you know the indentity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window
112+
When you know the identity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window
111113
112114
```PowerShell
113115
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
@@ -144,3 +146,21 @@ $tenantId = "yourTenantIdGuid"
144146
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
145147
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
146148
```
149+
150+
### Running the script on Azure Sovereign clouds
151+
152+
All the four options listed above, can be used on any Azure Sovereign clouds. By default, the script targets `AzureCloud`, but it can be changed using the parameter `-AzureEnvironmentName`.
153+
154+
The acceptable values for this parameter are:
155+
156+
- AzureCloud
157+
- AzureChinaCloud
158+
- AzureUSGovernment
159+
- AzureGermanyCloud
160+
161+
Example:
162+
163+
```PowerShell
164+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
165+
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
166+
```

AppCreationScripts/Cleanup.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
param(
33
[PSCredential] $Credential,
44
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5-
[string] $tenantId
5+
[string] $tenantId,
6+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script (it defaults to AzureCloud)')]
7+
[string] $azureEnvironmentName
68
)
79

810
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
911
Install-Module "AzureAD" -Scope CurrentUser
1012
}
1113
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
14+
$ErrorActionPreference = "Stop"
1315

1416
Function Cleanup
1517
{
16-
<#
17-
.Description
18-
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
19-
#>
18+
if (!$azureEnvironmentName)
19+
{
20+
$azureEnvironmentName = "AzureCloud"
21+
}
22+
23+
<#
24+
.Description
25+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
26+
#>
2027

2128
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
2229
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -25,17 +32,17 @@ This function removes the Azure AD applications for the sample. These applicatio
2532
# you'll need to sign-in with creds enabling your to create apps in the tenant)
2633
if (!$Credential -and $TenantId)
2734
{
28-
$creds = Connect-AzureAD -TenantId $tenantId
35+
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
2936
}
3037
else
3138
{
3239
if (!$TenantId)
3340
{
34-
$creds = Connect-AzureAD -Credential $Credential
41+
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
3542
}
3643
else
3744
{
38-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
45+
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
3946
}
4047
}
4148

AppCreationScripts/Configure.ps1

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
param(
33
[PSCredential] $Credential,
44
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5-
[string] $tenantId
5+
[string] $tenantId,
6+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script (it defaults to AzureCloud)')]
7+
[string] $azureEnvironmentName
68
)
79

810
<#
@@ -39,7 +41,7 @@ Function AddResourcePermission($requiredAccess, `
3941
}
4042

4143
#
42-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
44+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
4345
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
4446
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
4547
{
@@ -89,19 +91,61 @@ Function ReplaceSetting([string] $configFilePath, [string] $key, [string] $newVa
8991
$content.save($configFilePath)
9092
}
9193

94+
<#.Description
95+
This function creates a new Azure AD scope (OAuth2Permission) with default and provided values
96+
#>
97+
Function CreateScope( [string] $value, [string] $userConsentDisplayName, [string] $userConsentDescription, [string] $adminConsentDisplayName, [string] $adminConsentDescription)
98+
{
99+
$scope = New-Object Microsoft.Open.AzureAD.Model.OAuth2Permission
100+
$scope.Id = New-Guid
101+
$scope.Value = $value
102+
$scope.UserConsentDisplayName = $userConsentDisplayName
103+
$scope.UserConsentDescription = $userConsentDescription
104+
$scope.AdminConsentDisplayName = $adminConsentDisplayName
105+
$scope.AdminConsentDescription = $adminConsentDescription
106+
$scope.IsEnabled = $true
107+
$scope.Type = "User"
108+
return $scope
109+
}
110+
111+
<#.Description
112+
This function creates a new Azure AD AppRole with default and provided values
113+
#>
114+
Function CreateAppRole([string] $types, [string] $name, [string] $description)
115+
{
116+
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
117+
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
118+
$typesArr = $types.Split(',')
119+
foreach($type in $typesArr)
120+
{
121+
$appRole.AllowedMemberTypes.Add($type);
122+
}
123+
$appRole.DisplayName = $name
124+
$appRole.Id = New-Guid
125+
$appRole.IsEnabled = $true
126+
$appRole.Description = $description
127+
$appRole.Value = $name;
128+
return $appRole
129+
}
92130

93131
Set-Content -Value "<html><body><table>" -Path createdApps.html
94132
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
95133

134+
$ErrorActionPreference = "Stop"
135+
96136
Function ConfigureApplications
97137
{
98138
<#.Description
99139
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
100140
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
101141
so that they are consistent with the Applications parameters
102142
#>
103-
104143
$commonendpoint = "common"
144+
145+
if (!$azureEnvironmentName)
146+
{
147+
$azureEnvironmentName = "AzureCloud"
148+
}
105149

106150
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
107151
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -110,17 +154,17 @@ Function ConfigureApplications
110154
# you'll need to sign-in with creds enabling your to create apps in the tenant)
111155
if (!$Credential -and $TenantId)
112156
{
113-
$creds = Connect-AzureAD -TenantId $tenantId
157+
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
114158
}
115159
else
116160
{
117161
if (!$TenantId)
118162
{
119-
$creds = Connect-AzureAD -Credential $Credential
163+
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
120164
}
121165
else
122166
{
123-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
167+
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
124168
}
125169
}
126170

@@ -129,20 +173,24 @@ Function ConfigureApplications
129173
$tenantId = $creds.Tenant.Id
130174
}
131175

176+
177+
132178
$tenant = Get-AzureADTenantDetail
133179
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
134180

135-
# Get the user running the script
181+
# Get the user running the script to add the user as the app owner
136182
$user = Get-AzureADUser -ObjectId $creds.Account.Id
137183

138184
# Create the service AAD application
139185
Write-Host "Creating the AAD application (TodoListService-ManualJwt)"
186+
# create the application
140187
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService-ManualJwt" `
141188
-HomePage "https://localhost:44324" `
142189
-PublicClient $False
143190
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
144191
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -IdentifierUris $serviceIdentifierUri
145192

193+
# create the service principal of the newly created application
146194
$currentAppId = $serviceAadApplication.AppId
147195
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
148196

@@ -154,19 +202,52 @@ Function ConfigureApplications
154202
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
155203
}
156204

205+
# rename the user_impersonation scope if it exists to match the readme steps or add a new scope
206+
$scopes = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.OAuth2Permission]
207+
208+
if ($scopes.Count -ge 0)
209+
{
210+
# add all existing scopes first
211+
$serviceAadApplication.Oauth2Permissions | foreach-object { $scopes.Add($_) }
212+
213+
$scope = $serviceAadApplication.Oauth2Permissions | Where-Object { $_.Value -eq "User_impersonation" }
214+
215+
if ($scope -ne $null)
216+
{
217+
$scope.Value = "access_as_user"
218+
}
219+
else
220+
{
221+
# Add scope
222+
$scope = CreateScope -value "access_as_user" `
223+
-userConsentDisplayName "Access TodoListService-ManualJwt" `
224+
-userConsentDescription "Allow the application to access TodoListService-ManualJwt on your behalf." `
225+
-adminConsentDisplayName "Access TodoListService-ManualJwt" `
226+
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of the signed-in user."
227+
228+
$scopes.Add($scope)
229+
}
230+
}
231+
232+
# add/update scopes
233+
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -OAuth2Permission $scopes
234+
157235
Write-Host "Done creating the service application (TodoListService-ManualJwt)"
158236

159237
# URL of the AAD application in the Azure portal
160238
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
161239
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
162240
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService-ManualJwt</a></td></tr>" -Path createdApps.html
163241

242+
164243
# Create the client AAD application
165244
Write-Host "Creating the AAD application (TodoListClient-ManualJwt)"
245+
# create the application
166246
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient-ManualJwt" `
167-
-ReplyUrls "urn:ietf:wg:oauth:2.0:oob", "https://login.microsoftonline.com/common/oauth2/nativeclient" `
247+
-ReplyUrls "https://login.microsoftonline.com/common/oauth2/nativeclient" `
168248
-PublicClient $True
169249

250+
# create the service principal of the newly created application
170251
$currentAppId = $clientAadApplication.AppId
171252
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
172253

@@ -178,6 +259,7 @@ Function ConfigureApplications
178259
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
179260
}
180261

262+
181263
Write-Host "Done creating the client application (TodoListClient-ManualJwt)"
182264

183265
# URL of the AAD application in the Azure portal
@@ -190,7 +272,7 @@ Function ConfigureApplications
190272
# Add Required Resources Access (from 'client' to 'service')
191273
Write-Host "Getting access from 'client' to 'service'"
192274
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService-ManualJwt" `
193-
-requiredDelegatedPermissions "user_impersonation" `
275+
-requiredDelegatedPermissions "access_as_user" `
194276

195277
$requiredResourcesAccess.Add($requiredPermissions)
196278

@@ -201,25 +283,26 @@ Function ConfigureApplications
201283
# Update config file for 'service'
202284
$configFile = $pwd.Path + "\..\TodoListService-ManualJwt\Web.Config"
203285
Write-Host "Updating the sample code ($configFile)"
204-
ReplaceSetting -configFilePath $configFile -key "ida:TenantId" -newValue $tenantId
205-
ReplaceSetting -configFilePath $configFile -key "ida:Audience" -newValue $serviceIdentifierUri
206-
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $serviceAadApplication.AppId
286+
ReplaceSetting -configFilePath $configFile -key "ida:TenantId" -newValue ($tenantId)
287+
ReplaceSetting -configFilePath $configFile -key "ida:Audience" -newValue ($serviceIdentifierUri)
288+
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue ($serviceAadApplication.AppId)
207289

208290
# Update config file for 'client'
209291
$configFile = $pwd.Path + "\..\TodoListClient\App.Config"
210292
Write-Host "Updating the sample code ($configFile)"
211-
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue $tenantName
212-
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $clientAadApplication.AppId
213-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue $serviceIdentifierUri
214-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
293+
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue ($tenantName)
294+
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue ($clientAadApplication.AppId)
295+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue ($serviceIdentifierUri)
296+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue ($serviceAadApplication.HomePage)
215297

216298
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
217299
}
218300

219301
# Pre-requisites
220302
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
221303
Install-Module "AzureAD" -Scope CurrentUser
222-
}
304+
}
305+
223306
Import-Module AzureAD
224307

225308
# Run interactively (will ask you for the tenant ID)

AppCreationScripts/sample.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
"Name": "TodoListClient-ManualJwt",
2626
"Kind": "Desktop",
2727
"Audience": "AzureADMyOrg",
28-
"ReplyUrls": "urn:ietf:wg:oauth:2.0:oob,https://login.microsoftonline.com/common/oauth2/nativeclient",
28+
"ReplyUrls": "https://login.microsoftonline.com/common/oauth2/nativeclient",
2929
"IsPublicClient": true,
3030
"RequiredResourcesAccess": [
3131
{
3232
"Resource": "service",
33-
"DelegatedPermissions": [ "user_impersonation" ]
33+
"DelegatedPermissions": [ "access_as_user" ]
3434
}
3535
]
3636
}

0 commit comments

Comments
 (0)