Skip to content

Commit fc50768

Browse files
authored
Merge pull request #81 from Azure-Samples/basher-1-1
updated sample
2 parents 487ace1 + c1956b6 commit fc50768

File tree

20 files changed

+33961
-1344
lines changed

20 files changed

+33961
-1344
lines changed

1-Authentication/1-sign-in/App/authConfig.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,39 @@
55
*/
66

77
const msalConfig = {
8-
auth: {
9-
clientId: "Enter_the_Application_Id_Here", // This is the ONLY mandatory field that you need to supply.
10-
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", // Defaults to "https://login.microsoftonline.com/common"
11-
redirectUri: "Enter_the_Redirect_Uri_Here", // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href
12-
postLogoutRedirectUri: "Enter_the_Redirect_Uri_Here/signout", // Simply remove this line if you would like navigate to index page after logout.
13-
navigateToLoginRequestUrl: false, // If "true", will navigate back to the original request location before processing the auth code response.
14-
},
15-
cache: {
16-
cacheLocation: "localStorage", // Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO.
17-
storeAuthStateInCookie: false, // If you wish to store cache items in cookies as well as browser cache, set this to "true".
18-
},
19-
system: {
20-
loggerOptions: {
21-
loggerCallback: (level, message, containsPii) => {
22-
if (containsPii) {
23-
return;
24-
}
25-
switch (level) {
26-
case msal.LogLevel.Error:
27-
console.error(message);
28-
return;
29-
case msal.LogLevel.Info:
30-
console.info(message);
31-
return;
32-
case msal.LogLevel.Verbose:
33-
console.debug(message);
34-
return;
35-
case msal.LogLevel.Warning:
36-
console.warn(message);
37-
return;
38-
}
39-
}
40-
}
41-
}
8+
auth: {
9+
clientId: 'Enter_the_Application_Id_Here', // This is the ONLY mandatory field that you need to supply.
10+
authority: 'https://login.microsoftonline.com/Enter_the_Tenant_Info_Here', // Defaults to "https://login.microsoftonline.com/common"
11+
redirectUri: '/', // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href e.g. http://localhost:3000/
12+
navigateToLoginRequestUrl: true, // If "true", will navigate back to the original request location before processing the auth code response.
13+
},
14+
cache: {
15+
cacheLocation: 'sessionStorage', // Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO.
16+
storeAuthStateInCookie: false, // set this to true if you have to support IE
17+
},
18+
system: {
19+
loggerOptions: {
20+
loggerCallback: (level, message, containsPii) => {
21+
if (containsPii) {
22+
return;
23+
}
24+
switch (level) {
25+
case msal.LogLevel.Error:
26+
console.error(message);
27+
return;
28+
case msal.LogLevel.Info:
29+
console.info(message);
30+
return;
31+
case msal.LogLevel.Verbose:
32+
console.debug(message);
33+
return;
34+
case msal.LogLevel.Warning:
35+
console.warn(message);
36+
return;
37+
}
38+
},
39+
},
40+
},
4241
};
4342

4443
/**
@@ -64,6 +63,7 @@ const loginRequest = {
6463
// exporting config object for jest
6564
if (typeof exports !== 'undefined') {
6665
module.exports = {
67-
msalConfig: msalConfig,
66+
msalConfig: msalConfig,
67+
loginRequest: loginRequest,
6868
};
6969
}

1-Authentication/1-sign-in/App/authPopup.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ function signOut() {
8484

8585
// Choose which account to logout from by passing a username.
8686
const logoutRequest = {
87-
account: myMSALObj.getAccountByUsername(username)
87+
account: myMSALObj.getAccountByUsername(username),
88+
mainWindowRedirectUri: 'http://localhost:3000/signout',
89+
redirectUri: 'http://localhost:3000/redirect.html',
8890
};
8991

90-
myMSALObj.logout(logoutRequest);
92+
myMSALObj.logoutPopup(logoutRequest);
9193
}
9294

9395
selectAccount();

1-Authentication/1-sign-in/App/authRedirect.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ function signOut() {
9191

9292
// Choose which account to logout from by passing a username.
9393
const logoutRequest = {
94-
account: myMSALObj.getAccountByUsername(username)
94+
account: myMSALObj.getAccountByUsername(username),
95+
postLogoutRedirectUri: 'http://localhost:3000/signout', // Simply remove this line if you would like navigate to index page after logout.
96+
9597
};
9698

97-
myMSALObj.logout(logoutRequest);
99+
myMSALObj.logoutRedirect(logoutRequest);
98100
}

1-Authentication/1-sign-in/App/index.html

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
77
<title>Microsoft identity platform</title>
88
<link rel="SHORTCUT ICON" href="./favicon.svg" type="image/x-icon">
9+
<link rel="stylesheet" href="./styles.css">
10+
911

1012
<!-- msal.min.js can be used in the place of msal.js; included msal.js to make debug easy -->
11-
<script id="load-msal" src="https://alcdn.msauth.net/browser/2.15.0/js/msal-browser.js"
12-
integrity="sha384-dFzMiVGB5HpWZ+5w5VSif6jhWfNeplSw9ACYmQKZcY2azuT9kCxVWVI9HyfGdkHV"
13+
<script id="load-msal" src="https://alcdn.msauth.net/browser/2.31.0/js/msal-browser.js"
14+
integrity="sha384-BO4qQ2RTxj2akCJc7t6IdU9aRg6do4LGIkVVa01Hm33jxM+v2G+4q+vZjmOCywYq"
1315
crossorigin="anonymous"></script>
14-
15-
<!-- To help ensure reliability, Microsoft provides a second CDN -->
16-
<script type="text/javascript">
17-
if (typeof Msal === 'undefined') document.write(unescape("%3Cscript src='https://alcdn.msftauth.net/browser/2.15.0/js/msal-browser.js' type='text/javascript' crossorigin='anonymous' %3E%3C/script%3E"));
18-
</script>
1916

20-
<!-- adding Bootstrap 4 for UI components -->
21-
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
22-
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
17+
<!-- adding Bootstrap 5 for UI components -->
18+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
2319
</head>
2420

2521
<body>
26-
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
22+
<nav class="navbar navbar-expand-lg navbar-dark bg-primary navbarStyle">
2723
<a class="navbar-brand" href="/">Microsoft identity platform</a>
28-
<div class="btn-group ml-auto dropleft">
24+
<div class="collapse navbar-collapse justify-content-end">
2925
<button type="button" id="signIn" class="btn btn-secondary" onclick="signIn()">Sign-in</button>
3026
<button type="button" id="signOut" class="btn btn-success d-none" onclick="signOut()">Sign-out</button>
3127
</div>
@@ -53,15 +49,13 @@ <h5 id="welcome-div" class="card-header text-center d-none"></h5>
5349
</div>
5450

5551
<!-- importing bootstrap.js and supporting js libraries -->
56-
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
57-
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
58-
crossorigin="anonymous"></script>
59-
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
60-
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
61-
crossorigin="anonymous"></script>
62-
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
63-
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
64-
crossorigin="anonymous"></script>
52+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
53+
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
54+
</script>
55+
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
56+
57+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
58+
6559

6660
<!-- importing app scripts (load order is important) -->
6761
<script type="text/javascript" src="./authConfig.js"></script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
Blank page for redirect purposes. When using popup and silent APIs,
3+
we recommend setting the redirectUri to a blank page or a page that does not implement MSAL.
4+
For more information, please follow this link:
5+
https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/login-user.md#redirecturi-considerations
6+
-->
7+
<h1>MSAL Redirect</h1>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.navbarStyle {
2+
padding: .5rem 1rem !important;
3+
}
Lines changed: 33 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
1-
# Registering the sample apps with the Microsoft identity platform and updating the configuration files using PowerShell
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 as **Administrator** 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:
99

1010
```PowerShell
1111
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1212
```
1313

14-
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)
14+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
1515

1616
```PowerShell
1717
cd .\AppCreationScripts\
18-
.\Configure.ps1
18+
.\Configure.ps1 -TenantId "your test tenant's id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'"
1919
```
2020

21-
1. Open the Visual Studio solution and click start
22-
2321
### More details
2422

25-
The following paragraphs:
26-
27-
- [Registering the sample apps with the Microsoft identity platform and updating the configuration files using PowerShell](#registering-the-sample-apps-with-the-Microsoft-identity-platform-and-updating-the-configuration-files-using-PowerShell)
28-
- [Overview](#overview)
29-
- [Quick summary](#quick-summary)
30-
- [More details](#more-details)
31-
- [Goal of the provided scripts](#goal-of-the-provided-scripts)
32-
- [Presentation of the scripts](#presentation-of-the-scripts)
33-
- [Usage pattern for tests and DevOps scenarios](#usage-pattern-for-tests-and-DevOps-scenarios)
34-
- [How to use the app creation scripts?](#how-to-use-the-app-creation-scripts)
35-
- [Pre-requisites](#pre-requisites)
36-
- [Run the script and start running](#run-the-script-and-start-running)
37-
- [Four ways to run the script](#four-ways-to-run-the-script)
38-
- [Option 1 (interactive)](#option-1-interactive)
39-
- [Option 2 (non-interactive)](#option-2-non-interactive)
40-
- [Option 3 (Interactive, but create apps in a specified tenant)](#option-3-Interactive-but-create-apps-in-a-specified-tenant)
41-
- [Option 4 (non-interactive, and create apps in a specified tenant)](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
42-
- [Running the script on Azure Sovereign clouds](#running-the-script-on-Azure-Sovereign-clouds)
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)
4333

4434
## Goal of the provided scripts
4535

@@ -50,14 +40,14 @@ This sample comes with two PowerShell scripts, which automate the creation of th
5040
These scripts are:
5141

5242
- `Configure.ps1` which:
53-
- creates Azure AD applications and their related objects (permissions, dependencies, secrets),
54-
- 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.
5545
- 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:
5646
- the identifier of the application
5747
- the AppId of the application
5848
- the url of its registration in the [Azure portal](https://portal.azure.com).
5949

60-
- `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`).
6151

6252
### Usage pattern for tests and DevOps scenarios
6353

@@ -75,22 +65,23 @@ The `Configure.ps1` will stop if it tries to create an Azure AD application whic
7565
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
7666
```
7767
78-
1. ### (Optionally) install AzureAD PowerShell modules
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:
7971
80-
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:
72+
1. If you have never done it already, in the PowerShell window, install the Microsoft.Graph.Applications PowerShell modules. For this:
8173
82-
1. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
83-
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
74+
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select **Run as administrator**).
8475
2. Type:
8576
8677
```PowerShell
87-
Install-Module AzureAD
78+
Install-Module Microsoft.Graph.Applications
8879
```
8980
9081
or if you cannot be administrator on your machine, run:
9182
9283
```PowerShell
93-
Install-Module AzureAD -Scope CurrentUser
84+
Install-Module Microsoft.Graph.Applications -Scope CurrentUser
9485
```
9586
9687
### Run the script and start running
@@ -105,44 +96,29 @@ The scripts install the required PowerShell module (AzureAD) for the current use
10596
1. Open the Visual Studio solution, and in the solution's context menu, choose **Set Startup Projects**.
10697
1. select **Start** for the projects
10798
108-
You're done. this just works!
99+
You're done!
109100
110-
### Four ways to run the script
101+
### Two ways to run the script
111102
112103
We advise four ways of running the script:
113104
114105
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
115-
- non-interactive: you will provide credentials, and the scripts decide in which tenant to create the objects,
116-
- 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,
117-
- 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,
118107
119108
Here are the details on how to do this.
120109
121110
#### Option 1 (interactive)
122111
123-
- 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).
124113
- The script will be run as the signed-in user and will use the tenant in which the user is defined.
125114
126115
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.
127116
128-
#### Option 2 (non-interactive)
129-
130-
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
131-
132-
```PowerShell
133-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
134-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
135-
. .\Cleanup.ps1 -Credential $mycreds
136-
. .\Configure.ps1 -Credential $mycreds
137-
```
138-
139-
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from KeyVault.
140-
141-
#### Option 3 (Interactive, but create apps in a specified tenant)
117+
#### Option 2 (Interactive, but create apps in a specified tenant)
142118
143119
if you want to create the apps in a particular tenant, you can use the following option:
144120
145-
- open the [Azure portal](https://portal.azure.com)
121+
- Open the [Azure portal](https://portal.azure.com)
146122
- Select the Azure Active directory you are interested in (in the combo-box below your name on the top right of the browser window)
147123
- Find the "Active Directory" object in this tenant
148124
- Go to **Properties** and copy the content of the **Directory Id** property
@@ -154,32 +130,19 @@ $tenantId = "yourTenantIdGuid"
154130
. .\Configure.ps1 -TenantId $tenantId
155131
```
156132

157-
#### Option 4 (non-interactive, and create apps in a specified tenant)
158-
159-
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:
160-
161-
```PowerShell
162-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
163-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
164-
$tenantId = "yourTenantIdGuid"
165-
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
166-
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
167-
```
168-
169133
### Running the script on Azure Sovereign clouds
170134

171-
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`.
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`.
172136

173137
The acceptable values for this parameter are:
174138

175139
- AzureCloud
176140
- AzureChinaCloud
177141
- AzureUSGovernment
178-
- AzureGermanyCloud
179142

180143
Example:
181144

182145
```PowerShell
183-
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
184-
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
146+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureUSGovernment"
147+
. .\Configure.ps1 -AzureEnvironmentName "AzureUSGovernment"
185148
```

0 commit comments

Comments
 (0)