Skip to content

Commit 5abc6e2

Browse files
author
Kalyan Krishna
authored
Merge pull request #44 from Azure-Samples/kkrishna/updates2021
security warning removed
2 parents 3aca62f + 0907a1d commit 5abc6e2

25 files changed

+2963
-1543
lines changed

AppCreationScripts/AppCreationScripts.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
# Registering the sample apps with Microsoft identity platform and updating the configuration files using PowerShell scripts
1+
# Registering sample apps with the Microsoft identity platform and updating the configuration files using PowerShell
22

33
## Overview
44

55
### Quick summary
66

7-
1. On Windows run PowerShell and navigate to the root of the cloned directory
7+
1. On Windows, run PowerShell as **Administrator** and navigate to the root of the cloned directory
88
1. In PowerShell run:
9+
910
```PowerShell
1011
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1112
```
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)
13+
14+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
15+
1316
```PowerShell
1417
cd .\AppCreationScripts\
1518
.\Configure.ps1
1619
```
17-
1. Open the Visual Studio solution and click start
1820

1921
### More details
2022

21-
The following paragraphs:
22-
23-
- [Present the scripts](#presentation-of-the-scripts) and explain their [usage patterns](#usage-pattern-for-tests-and-devops-scenarios) for test and DevOps scenarios.
24-
- Explain the [pre-requisites](#pre-requisites)
25-
- Explain [four ways of running the scripts](#four-ways-to-run-the-script):
26-
- [Interactively](#option-1-interactive) to create the app in your home tenant
27-
- [Passing credentials](#option-2-non-interactive) to create the app in your home tenant
28-
- [Interactively in a specific tenant](#option-3-interactive-but-create-apps-in-a-specified-tenant)
29-
- [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)
31-
32-
## Goal of the scripts
23+
- [Goal of the provided scripts](#goal-of-the-provided-scripts)
24+
- [Presentation of the scripts](#presentation-of-the-scripts)
25+
- [Usage pattern for tests and DevOps scenarios](#usage-pattern-for-tests-and-DevOps-scenarios)
26+
- [How to use the app creation scripts?](#how-to-use-the-app-creation-scripts)
27+
- [Pre-requisites](#pre-requisites)
28+
- [Run the script and start running](#run-the-script-and-start-running)
29+
- [Four ways to run the script](#four-ways-to-run-the-script)
30+
- [Option 1 (interactive)](#option-1-interactive)
31+
- [Option 2 (non-interactive)](#option-2-non-interactive)
32+
- [Option 3 (Interactive, but create apps in a specified tenant)](#option-3-Interactive-but-create-apps-in-a-specified-tenant)
33+
- [Option 4 (non-interactive, and create apps in a specified tenant)](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
34+
- [Running the script on Azure Sovereign clouds](#running-the-script-on-Azure-Sovereign-clouds)
35+
36+
## Goal of the provided scripts
3337

3438
### Presentation of the scripts
3539

@@ -39,13 +43,13 @@ These scripts are:
3943

4044
- `Configure.ps1` which:
4145
- creates Azure AD applications and their related objects (permissions, dependencies, secrets),
42-
- changes the configuration files in the C# and JavaScript projects.
46+
- changes the configuration files in the sample projects.
4347
- 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:
4448
- the identifier of the application
4549
- the AppId of the application
4650
- the url of its registration in the [Azure portal](https://portal.azure.com).
4751

48-
- `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).
52+
- `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`).
4953

5054
### Usage pattern for tests and DevOps scenarios
5155

@@ -56,53 +60,60 @@ The `Configure.ps1` will stop if it tries to create an Azure AD application whic
5660
### Pre-requisites
5761

5862
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
59-
2. Navigate to the root directory of the project.
60-
3. Until you change it, the default [Execution Policy](https:/go.microsoft.com/fwlink/?LinkID=135170) for scripts is usually `Restricted`. In order to run the PowerShell script you need to set the Execution Policy to `RemoteSigned`. You can set this just for the current PowerShell process by running the command:
63+
1. Navigate to the root directory of the project.
64+
1. Until you change it, the default [Execution Policy](https:/go.microsoft.com/fwlink/?LinkID=135170) for scripts is usually `Restricted`. In order to run the PowerShell script you need to set the Execution Policy to `RemoteSigned`. You can set this just for the current PowerShell process by running the command:
65+
6166
```PowerShell
6267
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
6368
```
69+
6470
### (Optionally) install AzureAD PowerShell modules
71+
6572
The scripts install the required PowerShell module (AzureAD) for the current user if needed. However, if you want to install if for all users on the machine, you can follow the following steps:
6673
67-
4. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
74+
1. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
6875
69-
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
76+
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select **Run as administrator**).
7077
2. Type:
78+
7179
```PowerShell
7280
Install-Module AzureAD
7381
```
7482
7583
or if you cannot be administrator on your machine, run:
84+
7685
```PowerShell
7786
Install-Module AzureAD -Scope CurrentUser
7887
```
7988
8089
### Run the script and start running
8190
82-
5. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
91+
1. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
92+
8393
```PowerShell
8494
cd AppCreationScripts
8595
```
86-
6. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
87-
7. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
88-
8. select **Start** for the projects
8996
90-
You're done. this just works!
97+
1. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
98+
1. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
99+
1. select **Start** for the projects
100+
101+
You're done!
91102
92103
### Four ways to run the script
93104
94105
We advise four ways of running the script:
95106
96107
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
97108
- non-interactive: you will provide credentials, and the scripts decide in which tenant to create the objects,
98-
- Interactive in specific tenant: you will provide the tenant in which you want to create the objects and then you will be prompted for credentials, and the scripts will create the objects,
99-
- non-interactive in specific tenant: you will provide tenant in which you want to create the objects and credentials, and the scripts will create the objects.
109+
- Interactive in specific tenant: you will provide the tenant in which you want to create the objects and then you will be prompted for credentials, and the scripts will create the objects,
110+
- non-interactive in specific tenant: you will provide the tenant in which you want to create the objects and credentials, and the scripts will create the objects.
100111
101112
Here are the details on how to do this.
102113
103114
#### Option 1 (interactive)
104115
105-
- Just run ``. .\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
116+
- Just run ``.\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
106117
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
107118
108119
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.
@@ -118,12 +129,13 @@ $mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantN
118129
. .\Configure.ps1 -Credential $mycreds
119130
```
120131

121-
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from KeyVault.
132+
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from **Azure Key Vault**.
122133

123134
#### Option 3 (Interactive, but create apps in a specified tenant)
124135

125136
if you want to create the apps in a particular tenant, you can use the following option:
126-
- open the [Azure portal](https://portal.azure.com)
137+
138+
- Open the [Azure portal](https://portal.azure.com)
127139
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window)
128140
- Find the "Active Directory" object in this tenant
129141
- Go to **Properties** and copy the content of the **Directory Id** property
@@ -149,7 +161,7 @@ $tenantId = "yourTenantIdGuid"
149161

150162
### Running the script on Azure Sovereign clouds
151163

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`.
164+
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`.
153165

154166
The acceptable values for this parameter are:
155167

AppCreationScripts/Cleanup.ps1

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ param(
77
[string] $azureEnvironmentName
88
)
99

10+
#Requires -Modules AzureAD -RunAsAdministrator
11+
12+
1013
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
11-
Install-Module "AzureAD" -Scope CurrentUser
14+
Install-Module "AzureAD" -Scope CurrentUser
1215
}
1316
Import-Module AzureAD
1417
$ErrorActionPreference = "Stop"
@@ -57,7 +60,14 @@ Function Cleanup
5760
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5861

5962
Write-Host "Removing 'service' (TodoListService-ManualJwt) if needed"
60-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
63+
try
64+
{
65+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
66+
}
67+
catch
68+
{
69+
Write-Host "Unable to remove the 'TodoListService-ManualJwt' . Try deleting manually." -ForegroundColor White -BackgroundColor Red
70+
}
6171
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-ManualJwt'"
6272
if ($apps)
6373
{
@@ -70,10 +80,23 @@ Function Cleanup
7080
Write-Host "Removed TodoListService-ManualJwt.."
7181
}
7282
# also remove service principals of this app
73-
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService-ManualJwt'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
74-
83+
try
84+
{
85+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService-ManualJwt'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
86+
}
87+
catch
88+
{
89+
Write-Host "Unable to remove ServicePrincipal 'TodoListService-ManualJwt' . Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
90+
}
7591
Write-Host "Removing 'client' (TodoListClient-ManualJwt) if needed"
76-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
92+
try
93+
{
94+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
95+
}
96+
catch
97+
{
98+
Write-Host "Unable to remove the 'TodoListClient-ManualJwt' . Try deleting manually." -ForegroundColor White -BackgroundColor Red
99+
}
77100
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'"
78101
if ($apps)
79102
{
@@ -86,8 +109,15 @@ Function Cleanup
86109
Write-Host "Removed TodoListClient-ManualJwt.."
87110
}
88111
# also remove service principals of this app
89-
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient-ManualJwt'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
90-
112+
try
113+
{
114+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient-ManualJwt'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
115+
}
116+
catch
117+
{
118+
Write-Host "Unable to remove ServicePrincipal 'TodoListClient-ManualJwt' . Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
119+
}
91120
}
92121

93-
Cleanup -Credential $Credential -tenantId $TenantId
122+
Cleanup -Credential $Credential -tenantId $TenantId
123+

AppCreationScripts/Configure.ps1

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ param(
77
[string] $azureEnvironmentName
88
)
99

10+
#Requires -Modules AzureAD -RunAsAdministrator
11+
1012
<#
1113
This script creates the Azure AD applications needed for this sample and updates the configuration files
1214
for the visual Studio projects from the data in the Azure AD applications.
@@ -187,6 +189,7 @@ Function ConfigureApplications
187189
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService-ManualJwt" `
188190
-HomePage "https://localhost:44324" `
189191
-PublicClient $False
192+
190193
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
191194
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -IdentifierUris $serviceIdentifierUri
192195

@@ -205,28 +208,30 @@ Function ConfigureApplications
205208
# rename the user_impersonation scope if it exists to match the readme steps or add a new scope
206209
$scopes = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.OAuth2Permission]
207210

208-
if ($scopes.Count -ge 0)
211+
# delete default scope i.e. User_impersonation
212+
$scope = $serviceAadApplication.Oauth2Permissions | Where-Object { $_.Value -eq "User_impersonation" }
213+
if($scope -ne $null)
209214
{
210-
# add all existing scopes first
211-
$serviceAadApplication.Oauth2Permissions | foreach-object { $scopes.Add($_) }
212-
213-
$scope = $serviceAadApplication.Oauth2Permissions | Where-Object { $_.Value -eq "User_impersonation" }
215+
# disable the scope
216+
$scope.IsEnabled = $false
217+
$scopes.Add($scope)
218+
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -Oauth2Permissions $scopes
219+
220+
# clear the scope
221+
$scopes.Clear()
222+
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -Oauth2Permissions $scopes
223+
}
214224

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" `
225+
if ($scopes.Count -ge 0)
226+
{
227+
$scope = CreateScope -value access_as_user `
223228
-userConsentDisplayName "Access TodoListService-ManualJwt" `
224229
-userConsentDescription "Allow the application to access TodoListService-ManualJwt on your behalf." `
225230
-adminConsentDisplayName "Access TodoListService-ManualJwt" `
226231
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of the signed-in user."
227232

228-
$scopes.Add($scope)
229-
}
233+
$scopes.Add($scope)
234+
230235
}
231236

232237
# add/update scopes
@@ -294,7 +299,13 @@ Function ConfigureApplications
294299
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue ($clientAadApplication.AppId)
295300
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue ($serviceIdentifierUri)
296301
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue ($serviceAadApplication.HomePage)
297-
302+
if($isOpenSSL -eq 'Y')
303+
{
304+
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
305+
Write-Host "You have generated certificate using OpenSSL so follow below steps: "
306+
Write-Host "Install the certificate on your system from current folder."
307+
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
308+
}
298309
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
299310
}
300311

AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Sample": {
3-
"Title": "How to manually validate a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)",
3+
"Title": "How to manually validate a JWT access token using the Microsoft identity platform ",
44
"Level": 300,
55
"Client": ".NET Desktop App (WPF)",
66
"Service": "ASP.NET Web API",

0 commit comments

Comments
 (0)