Skip to content

Commit e03be35

Browse files
authored
Merge pull request #1 from Azure-Samples/web_app_sample
Web app sample
2 parents 5ab4a4b + 980f2bc commit e03be35

File tree

11 files changed

+809
-38
lines changed

11 files changed

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

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 'pythonwebapp' (python-webapp) if needed"
53+
Get-AzureADApplication -Filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'python-webapp'"
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 python-webapp.."
64+
}
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
68+
}
69+
70+
Cleanup -Credential $Credential -tenantId $TenantId

0 commit comments

Comments
 (0)