Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 771d1b3

Browse files
committed
2 parents ad53473 + d232549 commit 771d1b3

File tree

9 files changed

+970
-39
lines changed

9 files changed

+970
-39
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Registering sample apps with the Microsoft identity platform and updating configuration files using PowerShell
2+
3+
## Overview
4+
5+
### Quick summary
6+
7+
1. On Windows, run PowerShell as **Administrator** and navigate to the root of the cloned directory
8+
1. In PowerShell run:
9+
10+
```PowerShell
11+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
12+
```
13+
14+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
15+
16+
```PowerShell
17+
cd .\AppCreationScripts\
18+
.\Configure.ps1 -TenantId "your test tenant's id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'"
19+
```
20+
21+
### More details
22+
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)
33+
34+
## Goal of the provided scripts
35+
36+
### Presentation of the scripts
37+
38+
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.
39+
40+
These scripts are:
41+
42+
- `Configure.ps1` which:
43+
- creates Azure AD applications and their related objects (permissions, dependencies, secrets, app roles),
44+
- changes the configuration files in the sample projects.
45+
- 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:
46+
- the identifier of the application
47+
- the AppId of the application
48+
- the url of its registration in the [Azure portal](https://portal.azure.com).
49+
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`).
51+
52+
### Usage pattern for tests and DevOps scenarios
53+
54+
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.
55+
56+
## How to use the app creation scripts?
57+
58+
### Pre-requisites
59+
60+
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
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+
64+
```PowerShell
65+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
66+
```
67+
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:
71+
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**).
75+
2. Type:
76+
77+
```PowerShell
78+
Install-Module Microsoft.Graph.Applications
79+
```
80+
81+
or if you cannot be administrator on your machine, run:
82+
83+
```PowerShell
84+
Install-Module Microsoft.Graph.Applications -Scope CurrentUser
85+
```
86+
87+
### Run the script and start running
88+
89+
1. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo,
90+
91+
```PowerShell
92+
cd AppCreationScripts
93+
```
94+
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!
100+
101+
### Two ways to run the script
102+
103+
We advise four ways of running the script:
104+
105+
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to 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,
107+
108+
Here are the details on how to do this.
109+
110+
#### Option 1 (interactive)
111+
112+
- Just run ``.\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
113+
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
114+
115+
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.
116+
117+
#### Option 2 (Interactive, but create apps in a specified tenant)
118+
119+
if you want to create the apps in a particular tenant, you can use the following option:
120+
121+
- Open the [Azure portal](https://portal.azure.com)
122+
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window)
123+
- Find the "Active Directory" object in this tenant
124+
- Go to **Properties** and copy the content of the **Directory Id** property
125+
- Then use the full syntax to run the scripts:
126+
127+
```PowerShell
128+
$tenantId = "yourTenantIdGuid"
129+
. .\Cleanup.ps1 -TenantId $tenantId
130+
. .\Configure.ps1 -TenantId $tenantId
131+
```
132+
133+
### Running the script on Azure Sovereign clouds
134+
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`.
136+
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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
2+
[CmdletBinding()]
3+
param(
4+
[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,
6+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script. Default = Global')]
7+
[string] $azureEnvironmentName
8+
)
9+
10+
11+
Function Cleanup
12+
{
13+
if (!$azureEnvironmentName)
14+
{
15+
$azureEnvironmentName = "Global"
16+
}
17+
18+
<#
19+
.Description
20+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
21+
#>
22+
23+
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
24+
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
25+
26+
# Connect to the Microsoft Graph API
27+
Write-Host "Connecting to Microsoft Graph"
28+
if ($tenantId -eq "")
29+
{
30+
Connect-MgGraph -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
31+
$tenantId = (Get-MgContext).TenantId
32+
}
33+
else
34+
{
35+
Connect-MgGraph -TenantId $tenantId -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
36+
}
37+
38+
# Removes the applications
39+
Write-Host "Cleaning-up applications from tenant '$tenantId'"
40+
41+
Write-Host "Removing 'service' (msal-node-api) if needed"
42+
try
43+
{
44+
Get-MgApplication -Filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
45+
}
46+
catch
47+
{
48+
$message = $_
49+
Write-Warning $Error[0]
50+
Write-Host "Unable to remove the application 'msal-node-api'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
51+
}
52+
53+
Write-Host "Making sure there are no more (msal-node-api) applications found, will remove if needed..."
54+
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-node-api'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
55+
56+
if ($apps)
57+
{
58+
Remove-MgApplication -ApplicationId $apps.Id
59+
}
60+
61+
foreach ($app in $apps)
62+
{
63+
Remove-MgApplication -ApplicationId $app.Id
64+
Write-Host "Removed msal-node-api.."
65+
}
66+
67+
# also remove service principals of this app
68+
try
69+
{
70+
Get-MgServicePrincipal -filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
71+
}
72+
catch
73+
{
74+
$message = $_
75+
Write-Warning $Error[0]
76+
Write-Host "Unable to remove ServicePrincipal 'msal-node-api'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
77+
}
78+
Write-Host "Removing 'client' (msal-react-spa) if needed"
79+
try
80+
{
81+
Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
82+
}
83+
catch
84+
{
85+
$message = $_
86+
Write-Warning $Error[0]
87+
Write-Host "Unable to remove the application 'msal-react-spa'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
88+
}
89+
90+
Write-Host "Making sure there are no more (msal-react-spa) applications found, will remove if needed..."
91+
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
92+
93+
if ($apps)
94+
{
95+
Remove-MgApplication -ApplicationId $apps.Id
96+
}
97+
98+
foreach ($app in $apps)
99+
{
100+
Remove-MgApplication -ApplicationId $app.Id
101+
Write-Host "Removed msal-react-spa.."
102+
}
103+
104+
# also remove service principals of this app
105+
try
106+
{
107+
Get-MgServicePrincipal -filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
108+
}
109+
catch
110+
{
111+
$message = $_
112+
Write-Warning $Error[0]
113+
Write-Host "Unable to remove ServicePrincipal 'msal-react-spa'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
114+
}
115+
}
116+
117+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) {
118+
Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser
119+
}
120+
Import-Module Microsoft.Graph.Applications
121+
$ErrorActionPreference = "Stop"
122+
123+
124+
try
125+
{
126+
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
127+
}
128+
catch
129+
{
130+
$_.Exception.ToString() | out-host
131+
$message = $_
132+
Write-Warning $Error[0]
133+
Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red
134+
}
135+
136+
Write-Host "Disconnecting from tenant"
137+
Disconnect-MgGraph

0 commit comments

Comments
 (0)