Skip to content

Commit 6fafa7e

Browse files
author
Tiago Brenck
authored
Merge pull request #166 from Azure-Samples/tibre/fix158
Fix issue 158 - Share SQL token cache between different apps
2 parents 09a4358 + 2ac2c07 commit 6fafa7e

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

2-WebApp-graph-user/2-2-TokenCache/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,19 @@ public void ConfigureServices(IServiceCollection services)
102102
The aforementioned four lines of code are explained below.
103103

104104
1. The first two lines enable MSAL.NET to hook-up to the OpenID Connect events to redeem the authorization code obtained by the ASP.NET Core middleware. After obtaining a token for Microsoft Graph, it saves it into the token cache, for use by the Controllers.
105-
1. The last two lines hook up the Sql server database based token caching solution to MSAL.NET. The Sql based token cache requires a **Connection string** named `TokenCacheDbConnStr` available in the **ConnectionStrings** collections of the **appsettings.json** configuration file.
105+
1. The last two lines hook up the Sql server database based token caching solution to MSAL.NET. The Sql based token cache requires a **Connection string** named `TokenCacheDbConnStr` available in the **ConnectionStrings** collections of the **appsettings.json** configuration file.
106106

107107
The files `MSALAppSqlTokenCacheProvider.cs` and `MSALPerUserSqlTokenCacheProvider` of the `Microsoft.Identity.Web` project contains the app and per-user token cache implementations that use Sql server as the token cache.
108108

109+
### Sharing the same Token Cache database between apps
110+
111+
Since we are using `IDataProtector` to protect the token being persisted on the database, in order to enable it to be used between different apps, `SetApplicationName()` must be configured with the same value for all apps. You can read [more details about IDataProtector here.](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2#setapplicationname)
112+
113+
```csharp
114+
services.AddDataProtection()
115+
.SetApplicationName("WebApp_Tutorial");
116+
```
117+
109118
## Next steps
110119

111120
- Learn how to enable distributed caches in [token cache serialization](../2.2.%20token%20cache%20serialization)

4-WebApp-your-API/Client/Controllers/TodoListController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
3-
using Microsoft.Identity.Client;
43
using Microsoft.Identity.Web.Client;
54
using System.Threading.Tasks;
65
using TodoListClient.Services;

Microsoft.Identity.Web/Client/TokenCacheProviders/Sql/MSALAppSqlTokenCacheProviderExtension.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public static IServiceCollection AddSqlAppTokenCache(this IServiceCollection ser
5252
{
5353
// Uncomment the following lines to create the database. In production scenarios, the database
5454
// will most probably be already present.
55-
/*
55+
/*
5656
var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
5757
tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString);
5858
5959
var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
6060
tokenCacheDbContextForCreation.Database.EnsureCreated();
61-
*/
61+
*/
6262
services.AddDataProtection();
6363

6464
services.AddDbContext<TokenCacheDbContext>(options =>
@@ -84,13 +84,18 @@ public static IServiceCollection AddSqlPerUserTokenCache(this IServiceCollection
8484
{
8585
// Uncomment the following lines to create the database. In production scenarios, the database
8686
// will most probably be already present.
87-
//var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
88-
//tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString);
87+
/*
88+
var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
89+
tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString);
8990
90-
//var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
91-
//tokenCacheDbContextForCreation.Database.EnsureCreated();
91+
var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
92+
tokenCacheDbContextForCreation.Database.EnsureCreated();
93+
*/
9294

93-
services.AddDataProtection();
95+
// To share protected payloads among apps, configure SetApplicationName in each app with the same value.
96+
// https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2#setapplicationname
97+
services.AddDataProtection()
98+
.SetApplicationName("WebApp_Tutorial");
9499

95100
services.AddDbContext<TokenCacheDbContext>(options =>
96101
options.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString));

0 commit comments

Comments
 (0)