Skip to content

Commit 6593bc8

Browse files
author
Kalyan Krishna
authored
Merge pull request #594 from Azure-Samples/update-multitenant-routes
Update multitenant routes
2 parents a504ecf + 6773309 commit 6593bc8

File tree

14 files changed

+814
-734
lines changed

14 files changed

+814
-734
lines changed
Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
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 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
14-
cd .\AppCreationScripts\
15-
.\Configure.ps1
17+
cd .\AppCreationScripts\
18+
.\Configure.ps1 -TenantId "your test tenant's id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'"
1619
```
17-
1. Open the Visual Studio solution and click start
1820

1921
### More details
2022

21-
The following paragraphs:
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 (Interactive, but create apps in a specified tenant)](#option-3-Interactive-but-create-apps-in-a-specified-tenant)
32+
- [Running the script on Azure Sovereign clouds](#running-the-script-on-Azure-Sovereign-clouds)
2233

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-
31-
## Goal of the scripts
34+
## Goal of the provided scripts
3235

3336
### Presentation of the scripts
3437

@@ -37,92 +40,85 @@ This sample comes with two PowerShell scripts, which automate the creation of th
3740
These scripts are:
3841

3942
- `Configure.ps1` which:
40-
- creates Azure AD applications and their related objects (permissions, dependencies, secrets),
41-
- changes the configuration files in the C# and JavaScript projects.
43+
- creates Azure AD applications and their related objects (permissions, dependencies, secrets, app roles),
44+
- changes the configuration files in the sample projects.
4245
- 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:
4346
- the identifier of the application
4447
- the AppId of the application
4548
- the url of its registration in the [Azure portal](https://portal.azure.com).
4649

47-
- `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).
50+
- `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`).
4851

4952
### Usage pattern for tests and DevOps scenarios
5053

5154
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.
5255

53-
## How to use the app creation scripts ?
56+
## How to use the app creation scripts?
5457

5558
### Pre-requisites
5659

5760
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
58-
2. Navigate to the root directory of the project.
59-
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:
61+
1. Navigate to the root directory of the project.
62+
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:
63+
6064
```PowerShell
6165
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
6266
```
63-
### (Optionally) install AzureAD PowerShell modules
64-
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:
6567
66-
4. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
68+
### (Optionally) install Microsoft.Graph.Applications PowerShell modules
69+
70+
The scripts install the required PowerShell module (Microsoft.Graph.Applications) 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:
6771
68-
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
72+
1. If you have never done it already, in the PowerShell window, install the Microsoft.Graph.Applications PowerShell modules. For this:
73+
74+
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select **Run as administrator**).
6975
2. Type:
76+
7077
```PowerShell
71-
Install-Module AzureAD
78+
Install-Module Microsoft.Graph.Applications
7279
```
7380
7481
or if you cannot be administrator on your machine, run:
82+
7583
```PowerShell
76-
Install-Module AzureAD -Scope CurrentUser
84+
Install-Module Microsoft.Graph.Applications -Scope CurrentUser
7785
```
7886
7987
### Run the script and start running
8088
81-
5. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
89+
1. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
90+
8291
```PowerShell
8392
cd AppCreationScripts
8493
```
85-
6. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
86-
7. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
87-
8. select **Start** for the projects
8894
89-
You're done. this just works!
95+
1. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
96+
1. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
97+
1. select **Start** for the projects
98+
99+
You're done!
90100
91-
### Four ways to run the script
101+
### Two ways to run the script
92102
93103
We advise four ways of running the script:
94104
95105
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
96-
- non-interactive: you will provide credentials, and the scripts decide in which tenant to create the objects,
97-
- 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,
98-
- 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.
106+
- 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,
99107
100108
Here are the details on how to do this.
101109
102110
#### Option 1 (interactive)
103111
104-
- Just run ``. .\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
112+
- Just run ``.\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
105113
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
106114
107115
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.
108116
109-
#### Option 2 (non-interactive)
110-
111-
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-
113-
```PowerShell
114-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
115-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
116-
. .\Cleanup.ps1 -Credential $mycreds
117-
. .\Configure.ps1 -Credential $mycreds
118-
```
119-
120-
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from KeyVault.
121-
122-
#### Option 3 (Interactive, but create apps in a specified tenant)
117+
#### Option 2 (Interactive, but create apps in a specified tenant)
123118
124119
if you want to create the apps in a particular tenant, you can use the following option:
125-
- open the [Azure portal](https://portal.azure.com)
120+
121+
- Open the [Azure portal](https://portal.azure.com)
126122
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window)
127123
- Find the "Active Directory" object in this tenant
128124
- Go to **Properties** and copy the content of the **Directory Id** property
@@ -134,14 +130,19 @@ $tenantId = "yourTenantIdGuid"
134130
. .\Configure.ps1 -TenantId $tenantId
135131
```
136132

137-
#### Option 4 (non-interactive, and create apps in a specified tenant)
133+
### Running the script on Azure Sovereign clouds
138134

139-
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:
135+
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`.
140136

141-
```PowerShell
142-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
143-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
144-
$tenantId = "yourTenantIdGuid"
145-
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
146-
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
147-
```
137+
The acceptable values for this parameter are:
138+
139+
- AzureCloud
140+
- AzureChinaCloud
141+
- AzureUSGovernment
142+
143+
Example:
144+
145+
```PowerShell
146+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureUSGovernment"
147+
. .\Configure.ps1 -AzureEnvironmentName "AzureUSGovernment"
148+
```
Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,87 @@
1+
12
[CmdletBinding()]
23
param(
3-
[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. Default = Global')]
7+
[string] $azureEnvironmentName
68
)
79

8-
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
9-
Install-Module "AzureAD" -Scope CurrentUser
10-
}
11-
Import-Module AzureAD
12-
$ErrorActionPreference = "Stop"
13-
1410
Function Cleanup
1511
{
16-
<#
17-
.Description
18-
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
19-
#>
12+
if (!$azureEnvironmentName)
13+
{
14+
$azureEnvironmentName = "Global"
15+
}
16+
17+
<#
18+
.Description
19+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
20+
#>
2021

2122
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
2223
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
2324

24-
# Login to Azure PowerShell (interactive if credentials are not already provided:
25-
# you'll need to sign-in with creds enabling your to create apps in the tenant)
26-
if (!$Credential -and $TenantId)
27-
{
28-
$creds = Connect-AzureAD -TenantId $tenantId
25+
# Connect to the Microsoft Graph API
26+
Write-Host "Connecting to Microsoft Graph"
27+
if ($tenantId -eq "") {
28+
Connect-MgGraph -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
29+
$tenantId = (Get-MgContext).TenantId
2930
}
30-
else
31-
{
32-
if (!$TenantId)
33-
{
34-
$creds = Connect-AzureAD -Credential $Credential
35-
}
36-
else
37-
{
38-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
39-
}
40-
}
41-
42-
if (!$tenantId)
43-
{
44-
$tenantId = $creds.Tenant.Id
31+
else {
32+
Connect-MgGraph -TenantId $tenantId -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
4533
}
46-
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4834

4935
# Removes the applications
50-
Write-Host "Cleaning-up applications from tenant '$tenantName'"
36+
Write-Host "Cleaning-up applications from tenant '$tenantId'"
5137

5238
Write-Host "Removing 'webApp' (WebApp-MultiTenant-v2) if needed"
53-
Get-AzureADApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'"
39+
try
40+
{
41+
Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
42+
}
43+
catch
44+
{
45+
$message = $_
46+
Write-Warning $Error[0]
47+
Write-Host "Unable to remove the application 'WebApp-MultiTenant-v2'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
48+
}
49+
50+
Write-Host "Making sure there are no more (WebApp-MultiTenant-v2) applications found, will remove if needed..."
51+
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
52+
5553
if ($apps)
5654
{
57-
Remove-AzureADApplication -ObjectId $apps.ObjectId
55+
Remove-MgApplication -ApplicationId $apps.Id
5856
}
5957

6058
foreach ($app in $apps)
6159
{
62-
Remove-AzureADApplication -ObjectId $app.ObjectId
60+
Remove-MgApplication -ApplicationId $app.Id -Debug
6361
Write-Host "Removed WebApp-MultiTenant-v2.."
6462
}
63+
6564
# also remove service principals of this app
66-
Get-AzureADServicePrincipal -filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67-
65+
try
66+
{
67+
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
68+
}
69+
catch
70+
{
71+
$message = $_
72+
Write-Warning $Error[0]
73+
Write-Host "Unable to remove ServicePrincipal 'WebApp-MultiTenant-v2'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
74+
}
6875
}
6976

70-
Cleanup -Credential $Credential -tenantId $TenantId
77+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) {
78+
Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser
79+
}
80+
Import-Module Microsoft.Graph.Applications
81+
$ErrorActionPreference = "Stop"
82+
83+
84+
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
85+
86+
Write-Host "Disconnecting from tenant"
87+
Disconnect-MgGraph

0 commit comments

Comments
 (0)