Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 27d5228

Browse files
authored
Merge pull request #2 from Azure-Samples/derisen
adding AppCreationScripts
2 parents c2e1b9d + 31f4267 commit 27d5228

File tree

7 files changed

+507
-4
lines changed

7 files changed

+507
-4
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Registering the sample apps with Microsoft identity platform and updating the configuration files using PowerShell scripts
2+
3+
## Overview
4+
5+
- [Quick summary](#quick-summary)
6+
- [Goal of the scripts](#goal-of-the-scripts)
7+
- [Presentation of the scripts](#presentation-of-the-scripts)
8+
- [Usage pattern for tests and DevOps scenarios](#usage-pattern-for-tests-and-devops-scenarios)
9+
- [How to use the app creation scripts](#how-to-use-the-app-creation-scripts)
10+
- [Pre-requisites](#pre-requisites)
11+
- [(Optionally) install AzureAD PowerShell modules](#optionally-install-azuread-powershell-modules)
12+
- [Run the script and start running](#run-the-script-and-start-running)
13+
- [Four ways to run the script](#four-ways-to-run-the-script)
14+
- [Option 1 (interactive)](#option-1-interactive)
15+
- [Option 2 (non-interactive)](#option-2-non-interactive)
16+
- [Option 3 (Interactive, but create apps in a specified tenant)](#option-3-interactive-but-create-apps-in-a-specified-tenant)
17+
- [Option 4 (non-interactive, and create apps in a specified tenant)](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
18+
19+
### Quick summary
20+
21+
1. On Windows run PowerShell and navigate to the root of the cloned directory.
22+
2. In PowerShell, run:
23+
24+
```PowerShell
25+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
26+
```
27+
28+
3. Run the script to create your Azure AD application and configure the code of the sample application accordingly:
29+
30+
```PowerShell
31+
cd .\AppCreationScripts\
32+
.\Configure.ps1
33+
```
34+
35+
4. Open the Visual Studio solution and click start.
36+
37+
> **NOTE** Other ways of running the scripts are described below
38+
39+
## Goal of the scripts
40+
41+
### Presentation of the scripts
42+
43+
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.
44+
45+
These scripts are:
46+
47+
- `Configure.ps1`, which creates Azure AD applications and their related objects (permissions, dependencies, secrets), and changes the configuration files in the C# and JavaScript projects.
48+
49+
- `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+
51+
### Usage pattern for tests and DevOps scenarios
52+
53+
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.
54+
55+
## How to use the app creation scripts
56+
57+
### Pre-requisites
58+
59+
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
60+
2. Navigate to the root directory of the project.
61+
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:
62+
63+
```PowerShell
64+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
65+
```
66+
67+
### (Optionally) install AzureAD PowerShell modules
68+
69+
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:
70+
71+
4. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
72+
73+
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
74+
2. Type:
75+
76+
```PowerShell
77+
Install-Module AzureAD
78+
```
79+
80+
or if you cannot be administrator on your machine, run:
81+
82+
```PowerShell
83+
Install-Module AzureAD -Scope CurrentUser
84+
```
85+
86+
### Run the script and start running
87+
88+
5. Go to the `AppCreationScripts` sub-folder. From the folder where you cloned the repo.
89+
90+
```PowerShell
91+
cd AppCreationScripts
92+
```
93+
94+
6. Run the scripts. See below for the [four options](#four-ways-to-run-the-script) to do that.
95+
7. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
96+
8. select **Start** for the projects
97+
98+
You're done. This just works!
99+
100+
### Four ways to run the script
101+
102+
We advise four ways of running the script:
103+
104+
- Interactive: you will be prompted for credentials, and the scripts decides in which tenant to create the objects.
105+
- non-interactive: you will provide 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+
- 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.
108+
109+
#### Option 1 (interactive)
110+
111+
- Just run ``. .\Configure.ps1``, and you will be prompted to sign-in (email address, password, and if needed MFA).
112+
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
113+
114+
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.
115+
116+
#### Option 2 (non-interactive)
117+
118+
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.
119+
120+
```PowerShell
121+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
122+
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
123+
..\Cleanup.ps1 -Credential $mycreds
124+
..\Configure.ps1 -Credential $mycreds
125+
```
126+
127+
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from `KeyVault`.
128+
129+
#### Option 3 (Interactive, but create apps in a specified tenant)
130+
131+
if you want to create the apps in a particular tenant, you can use the following option:
132+
133+
- open the [Azure portal](https://portal.azure.com).
134+
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window).
135+
- Find the "Active Directory" object in this tenant.
136+
- Go to **Properties** and copy the content of the **Directory Id** property.
137+
- Then use the full syntax to run the scripts:
138+
139+
```PowerShell
140+
$tenantId = "yourTenantIdGuid"
141+
..\Cleanup.ps1 -TenantId $tenantId
142+
..\Configure.ps1 -TenantId $tenantId
143+
```
144+
145+
#### Option 4 (non-interactive, and create apps in a specified tenant)
146+
147+
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:
148+
149+
```PowerShell
150+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
151+
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
152+
$tenantId = "yourTenantIdGuid"
153+
..\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
154+
..\Configure.ps1 -Credential $mycreds -TenantId $tenantId
155+
```

AppCreationScripts/Cleanup.ps1

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[CmdletBinding()]
2+
param(
3+
[PSCredential] $Credential,
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+
)
7+
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
9+
Install-Module "AzureAD" -Scope CurrentUser
10+
}
11+
Import-Module AzureAD
12+
$ErrorActionPreference = "Stop"
13+
14+
Function Cleanup
15+
{
16+
<#
17+
.Description
18+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
19+
#>
20+
21+
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
22+
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
23+
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
29+
}
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
45+
}
46+
$tenant = Get-AzureADTenantDetail
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
48+
49+
# Removes the applications
50+
Write-Host "Cleaning-up applications from tenant '$tenantName'"
51+
52+
Write-Host "Removing 'spa' (active-directory-javascript-singlepageapp-angular) if needed"
53+
Get-AzureADApplication -Filter "DisplayName eq 'active-directory-javascript-singlepageapp-angular'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'active-directory-javascript-singlepageapp-angular'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
59+
60+
foreach ($app in $apps)
61+
{
62+
Remove-AzureADApplication -ObjectId $app.ObjectId
63+
Write-Host "Removed active-directory-javascript-singlepageapp-angular.."
64+
}
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'active-directory-javascript-singlepageapp-angular'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
68+
}
69+
70+
Cleanup -Credential $Credential -tenantId $TenantId

0 commit comments

Comments
 (0)