Skip to content

Commit 515f1fc

Browse files
committed
Improving doc and app generation
- Improving the Configure.ps1 script which now generates the createdApps.html file which contains information about the created apps (name, URL to portal, AppId) - Adding the AppCreationScripts.md file to help using the app creation scripts - Improving the README.md (fixing md lint issues, and linking to AppCreationScripts.md)
1 parent 3392f66 commit 515f1fc

File tree

3 files changed

+155
-23
lines changed

3 files changed

+155
-23
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Registering the Azure Active Directory applications and updating the configuration files for this sample using PowerShell scripts
2+
3+
## Goal of the scripts
4+
5+
### Presentation of the scripts
6+
7+
This sample comes with two PowerShell scripts, which automate the creation of the Azure Active Directory applications, and the configuration of the code for this sample. Once you run them, you will only need to build the solution and you are good to test.
8+
9+
These scripts are:
10+
11+
- `Configure.ps1` which:
12+
- creates Azure AD applications and their related objects (permissions, dependencies, secrets),
13+
- changes the configuration files in the C# and JavaScript projects.
14+
- creates a summary file named `createdApps.html` in the folder from which you ran the script, and containing, for each Azure AD application it created, the identifier of the application, and url of its registration in the [Azure portal](https://portal.azure.com).
15+
16+
- `Cleanup.ps1` which cleans-up the Azure AD objects created by `Configure.ps1`. Note that this script does not revert the changes done in the configuration files, though. You will need to undo the change from source control (from Visual Studio, or from the command line using, for instance, git reset).
17+
18+
### Usage pattern for tests and DevOps scenarios
19+
20+
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.
21+
22+
## How to use the app creation scripts ?
23+
24+
### Pre-requisites
25+
26+
To use the app creation scripts:
27+
28+
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
29+
2. Navigate to the root directory of the project.
30+
3. Until you change it, the default Execution Policy for scripts is usually `Restricted`. In order to run the PowerShell script you need to set the Execution Policy to `Unrestricted`. You can set this just for the current PowerShell process by running the command:
31+
```PowerShell
32+
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
33+
```
34+
4. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this, type:
35+
```PowerShell
36+
Install-Module AzureAD
37+
```
38+
5. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
39+
```PowerShell
40+
cd AppCreationScripts
41+
```
42+
6. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
43+
7. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
44+
8. select **Start** for the projects
45+
46+
You're done. this just works!
47+
48+
### Four ways to run the script
49+
50+
We advise four ways of running the script:
51+
52+
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
53+
- non-interactive: you will provide crendentials, and the scripts decide in which tenant to create the objects,
54+
- Interactive in specific tenant: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
55+
- non-interactive in specific tenant: you will provide crendentials, and the scripts decide in which tenant to create the objects.
56+
57+
Here are the details on how to do this.
58+
59+
#### Option 1 (interactive)
60+
61+
- Just run ``. .\Configue.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
62+
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
63+
64+
Note that the script will choose the tenant in which to create the applications, based on the user. Also to run the `Cleanup.ps1` script, you will need to re-sign-in.
65+
66+
#### Option 2 (non-interactive)
67+
68+
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
69+
70+
```PowerShell
71+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
72+
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
73+
. .\Cleanup.ps1 -Credential $mycreds
74+
. .\Configure.ps1 -Credential $mycreds
75+
```
76+
77+
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from KeyVault.
78+
79+
#### Option 3 (Interactive, but create apps in a specified tenant)
80+
81+
if you want to create the apps in a particular tenant, you can use the following option:
82+
- open the [Azure portal](https://portal.azure.com)
83+
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window)
84+
- Find the "Active Directory" object in this tenant
85+
- Go to **Properties** and copy the content of the **Directory Id** property
86+
- Then use the full syntax to run the scripts:
87+
88+
```PowerShell
89+
$tenantId = "yourTenantIdGuid"
90+
. .\Cleanup.ps1 -TenantId $tenantId
91+
. .\Configure.ps1 -TenantId $tenantId
92+
```
93+
94+
#### Option 4 (non-interactive, and create apps in a specified tenant)
95+
96+
This option combines option 2 and option 3: it creates the application in a specific tenant. See option 3 for the way to get the tenant Id. Then run:
97+
98+
```PowerShell
99+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
100+
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
101+
$tenantId = "yourTenantIdGuid"
102+
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
103+
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
104+
```

AppCreationScripts/Configure.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ Function ReplaceSetting([string] $configFilePath, [string] $key, [string] $newVa
102102
}
103103

104104

105+
Set-Content -Value "<html><body><table>" -Path createdApps.html
106+
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
107+
105108
Function ConfigureApplications
106109
{
107110
<#.Description
@@ -152,17 +155,27 @@ Function ConfigureApplications
152155
-HomePage "https://localhost:44324" `
153156
-IdentifierUris "https://$tenantName/TodoListService-ManualJwt" `
154157
-PublicClient $False
155-
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $serviceAadApplication.AppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
158+
$currentAppId = $serviceAadApplication.AppId
159+
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
156160
Write-Host "Done."
157161

162+
# URL of the AAD application in the Azure portal
163+
$servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_IAM/ApplicationBlade/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId
164+
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService-ManualJwt</a></td></tr>" -Path createdApps.html
165+
158166
# Create the client AAD application
159167
Write-Host "Creating the AAD appplication (TodoListClient-ManualJwt)"
160168
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient-ManualJwt" `
161169
-ReplyUrls "https://TodoListClient-ManualJwt" `
162170
-PublicClient $True
163-
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $clientAadApplication.AppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
171+
$currentAppId = $clientAadApplication.AppId
172+
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
164173
Write-Host "Done."
165174

175+
# URL of the AAD application in the Azure portal
176+
$clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_IAM/ApplicationBlade/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId
177+
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient-ManualJwt</a></td></tr>" -Path createdApps.html
178+
166179
# Add Required Resources Access (from 'client' to 'service')
167180
Write-Host "Getting access from 'client' to 'service'"
168181
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
@@ -194,8 +207,11 @@ Function ConfigureApplications
194207
ReplaceSetting -configFilePath $configFile -key "ida:RedirectUri" -newValue $clientAadApplication.ReplyUrls
195208
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue $serviceAadApplication.IdentifierUris
196209
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
210+
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
211+
197212
}
198213
}
199214

215+
200216
# Run interactively (will ask you for the tenant ID)
201217
ConfigureApplications -Credential $Credential -tenantId $TenantId

0 commit comments

Comments
 (0)