Skip to content

Commit dd03fc8

Browse files
committed
mergin master to jmprieur/SimplifyTokenCacheProviders branch
2 parents bed6a6c + 801f2ed commit dd03fc8

File tree

62 files changed

+2038
-407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2038
-407
lines changed

1-WebApp-OIDC/1-1-MyOrg/README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,32 @@ To run this sample:
3232

3333
There is one project in this sample. To register it, you can:
3434

35-
- either use PowerShell scripts that **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you and modify the Visual Studio projects' configuration files. If you want to use this automation:
35+
- either use PowerShell scripts that **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you and modify the Visual Studio projects' configuration files.
3636

37-
1. On Windows run PowerShell and navigate to the solution's folder
38-
2. In PowerShell run:
37+
<details>
38+
<summary>Expand to see how to use this automation</summary>
3939

40-
```PowerShell
41-
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
42-
```
40+
1. On Windows run PowerShell and navigate to the solution's folder
4341

44-
3. Run the script to create your Azure AD application and configure the code of the sample application accordinly
42+
2. In PowerShell run:
4543

46-
```PowerShell
47-
.\AppCreationScripts\Configure.ps1
48-
```
44+
```PowerShell
45+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
46+
```
47+
48+
3. Run the script to create your Azure AD application and configure the code of the sample application accordingly
49+
50+
```PowerShell
51+
.\AppCreationScripts\Configure.ps1
52+
```
4953
50-
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
54+
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
5155
52-
4. Open the Visual Studio solution and click start. That's it!
56+
4. Open the Visual Studio solution and click start. That's it!
5357
54-
- or, if you don't want to use automation, follow the steps below:
58+
</details>
59+
60+
- or, if you want to register your application with the Azure portal, follow the steps below:
5561
5662
#### Choose the Azure AD tenant where you want to create your applications
5763
@@ -68,9 +74,23 @@ As a first step you'll need to:
6874
1. When the **Register an application page** appears, enter your application's registration information:
6975
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `WebApp`.
7076
- In the **Supported account types** section, select **Accounts in this organizational directory only ({tenant name})**.
77+
<details open=true>
78+
<summary>Expand/collapse screenshot</summary>
79+
80+
![Register app](../../ReadmeFiles/screenshot-register-app.png)
81+
82+
</details>
7183
> Note that there are more than one redirect URIs. You'll need to add them from the **Authentication** tab later after the app has been created succesfully.
84+
7285
1. Select **Register** to create the application.
7386
1. On the app **Overview** page, find the **Application (client) ID** value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
87+
<details open=true>
88+
<summary>Expand/collapse screenshot</summary>
89+
90+
![OVerview page](../../ReadmeFiles/screenshot-overview.png)
91+
92+
</details>
93+
7494
1. In the list of pages for the app, select **Authentication**..
7595
- In the Redirect URIs section, select **Web** in the combo-box and enter the following redirect URIs.
7696
- `https://localhost:44321/`
@@ -79,6 +99,13 @@ As a first step you'll need to:
7999
- In the **Advanced settings** | **Implicit grant** section, check **ID tokens** as this sample requires
80100
the [Implicit grant flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow) to be enabled to
81101
sign-in the user.
102+
<details open=true>
103+
<summary>Expand/collapse screenshot</summary>
104+
105+
![Authentication page](../../ReadmeFiles/screenshot-authentication.png)
106+
107+
</details>
108+
82109
1. Select **Save**.
83110
84111
> Note that unless the Web App calls a Web API, no certificate or secret is needed.

1-WebApp-OIDC/README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
---
2-
services: active-directory
3-
platforms: dotnet
4-
author: jmprieur
5-
level: 200
6-
client: ASP.NET Core .Web App
7-
service: Microsoft Graph, Azure Storage, ASP.NET Core Web API
8-
endpoint: Microsoft identity platform
9-
page_type: sample
10-
languages:
11-
- csharp
12-
products:
13-
- azure
14-
- azure-active-directory
15-
- dotnet
16-
- office-ms-graph
17-
- aspnet-core
18-
- azure-storage
19-
---
201
[![Build status](https://identitydivision.visualstudio.com/IDDP/_apis/build/status/AAD%20Samples/.NET%20client%20samples/ASP.NET%20Core%20Web%20App%20tutorial)](https://identitydivision.visualstudio.com/IDDP/_build/latest?definitionId=819)
212

223
# Tutorial Phase - Enable your Web Apps to sign-in users

2-WebApp-graph-user/2-1-Call-MSGraph/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ The two new lines of code:
104104
> - replace `using Microsoft.Identity.Web.TokenCacheProviders.InMemory` by `using Microsoft.Identity.Web.TokenCacheProviders.Session`
105105
> - Replace `.AddInMemoryTokenCaches()` by `.AddSessionTokenCaches()`
106106
> add `app.UseSession();` in the `Configure(IApplicationBuilder app, IHostingEnvironment env)` method, for instance after `app.UseCookiePolicy();`
107+
>
108+
>
109+
> You can also use a distributed token cache, and choose the serialization implementation. For this, in **Startup.cs**:
110+
> - replace `using Microsoft.Identity.Web.TokenCacheProviders.InMemory` by `using Microsoft.Identity.Web.TokenCacheProviders.Distributed`
111+
> - Replace `.AddInMemoryTokenCaches()` by `.AddDistributedTokenCaches()`
112+
> - Then choose the distributed cache implementation. For details, see https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.2#distributed-memory-cache
113+
>
114+
> ```CSharp
115+
> // use a distributed Token Cache by adding
116+
> .AddDistributedTokenCaches();
117+
>
118+
> // and then choose your implementation.
119+
>
120+
> // For instance the distributed in memory cache (not cleaned when you stop the app)
121+
> services.AddDistributedMemoryCache()
122+
>
123+
> // Or a Redis cache
124+
> services.AddStackExchangeRedisCache(options =>
125+
> {
126+
> options.Configuration = "localhost";
127+
> options.InstanceName = "SampleInstance";
128+
> });
129+
>
130+
> // Or even a SQL Server token cache
131+
> services.AddDistributedSqlServerCache(options =>
132+
> {
133+
> options.ConnectionString =_config["DistCache_ConnectionString"];
134+
> options.SchemaName = "dbo";
135+
> options.TableName = "TestCache";
136+
> });
137+
> ```
107138

108139
### Add additional files to call Microsoft Graph
109140

2-WebApp-graph-user/2-1-Call-MSGraph/Startup.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Identity.Web;
10+
using Microsoft.Identity.Web.TokenCacheProviders.Distributed;
1011
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
1112
using WebApp_OpenIDConnect_DotNet.Infrastructure;
1213
using WebApp_OpenIDConnect_DotNet.Services;
@@ -40,6 +41,32 @@ public void ConfigureServices(IServiceCollection services)
4041
.AddMsal(Configuration, new string[] { Constants.ScopeUserRead })
4142
.AddInMemoryTokenCaches();
4243

44+
/*
45+
// or use a distributed Token Cache by adding
46+
.AddDistributedTokenCaches();
47+
48+
// and then choose your implementation.
49+
// See https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.2#distributed-memory-cache
50+
51+
// For instance the distributed in memory cache
52+
services.AddDistributedMemoryCache()
53+
54+
// Or a Redis cache
55+
services.AddStackExchangeRedisCache(options =>
56+
{
57+
options.Configuration = "localhost";
58+
options.InstanceName = "SampleInstance";
59+
});
60+
61+
// Or even a SQL Server token cache
62+
services.AddDistributedSqlServerCache(options =>
63+
{
64+
options.ConnectionString =
65+
_config["DistCache_ConnectionString"];
66+
options.SchemaName = "dbo";
67+
options.TableName = "TestCache";
68+
});
69+
*/
4370
// Add Graph
4471
services.AddGraphService(Configuration);
4572

@@ -81,4 +108,4 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
81108
});
82109
}
83110
}
84-
}
111+
}

2-WebApp-graph-user/2-2-TokenCache/AppCreationScripts/Cleanup.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{

2-WebApp-graph-user/2-2-TokenCache/AppCreationScripts/Configure.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Function AddResourcePermission($requiredAccess, `
6565
}
6666

6767
#
68-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
68+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
6969
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
7070
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
7171
{
@@ -134,18 +134,18 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
134134
Set-Content -Path $configFilePath -Value $lines -Force
135135
}
136136

137-
138137
Set-Content -Value "<html><body><table>" -Path createdApps.html
139138
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
140139

140+
$ErrorActionPreference = "Stop"
141+
141142
Function ConfigureApplications
142143
{
143144
<#.Description
144145
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
145146
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
146147
so that they are consistent with the Applications parameters
147148
#>
148-
149149
$commonendpoint = "common"
150150

151151
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
@@ -177,7 +177,7 @@ Function ConfigureApplications
177177
$tenant = Get-AzureADTenantDetail
178178
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
179179

180-
# Get the user running the script
180+
# Get the user running the script to add the user as the app owner
181181
$user = Get-AzureADUser -ObjectId $creds.Account.Id
182182

183183
# Create the webApp AAD application
@@ -187,6 +187,7 @@ Function ConfigureApplications
187187
$fromDate = [DateTime]::Now;
188188
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
189189
$webAppAppKey = $pw
190+
# create the application
190191
$webAppAadApplication = New-AzureADApplication -DisplayName "WebApp-OpenIDConnect-DotNet-code-v2" `
191192
-HomePage "https://localhost:44321/" `
192193
-LogoutUrl "https://localhost:44321/signout-oidc" `
@@ -197,6 +198,7 @@ Function ConfigureApplications
197198
-Oauth2AllowImplicitFlow $true `
198199
-PublicClient $False
199200

201+
# create the service principal of the newly created application
200202
$currentAppId = $webAppAadApplication.AppId
201203
$webAppServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
202204

@@ -241,7 +243,8 @@ Function ConfigureApplications
241243
# Pre-requisites
242244
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
243245
Install-Module "AzureAD" -Scope CurrentUser
244-
}
246+
}
247+
245248
Import-Module AzureAD
246249

247250
# Run interactively (will ask you for the tenant ID)

2-WebApp-graph-user/2-2-TokenCache/AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Sample": {
3-
"Title": "Using the Microsoft identity platform to call the Microsoft Graph API from an An ASP.NET Core 2.x Web App, on behalf of a user signing-in using their work and school or Microsoft personal account",
3+
"Title": "Call the Microsoft Graph API from an An ASP.NET Core 2.x Web App, using Sql Server for caching tokens",
44
"Level": 200,
55
"Client": "ASP.NET Core 2.x Web App",
66
"Service": "Microsoft Graph",

0 commit comments

Comments
 (0)