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

Commit 23f529b

Browse files
authored
Merge pull request #75 from Azure-Samples/jennyf/4.3
update to 4.3
2 parents 27421c4 + e8e4839 commit 23f529b

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

WebApp-OpenIDConnect-DotNet/Models/MSALSessionCache.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@ public class MSALSessionCache
1111
private static ReaderWriterLockSlim SessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
1212
string UserId = string.Empty;
1313
string CacheId = string.Empty;
14-
HttpContext httpContext = null;
15-
16-
ITokenCache cache;
14+
private readonly HttpContext httpContext = null;
15+
private ITokenCache cache;
1716

1817
public MSALSessionCache(string userId, HttpContext httpcontext)
1918
{
2019
// not object, we want the SUB
2120
UserId = userId;
2221
CacheId = UserId + "_TokenCache";
2322
httpContext = httpcontext;
24-
Load();
2523
}
2624

2725
public ITokenCache EnablePersistence(ITokenCache cache)
2826
{
2927
this.cache = cache;
3028
cache.SetBeforeAccess(BeforeAccessNotification);
3129
cache.SetAfterAccess(AfterAccessNotification);
32-
Load();
3330
return cache;
3431
}
3532

@@ -47,31 +44,31 @@ public string ReadUserStateValue()
4744
SessionLock.ExitReadLock();
4845
return state;
4946
}
50-
public void Load()
47+
public void Load(TokenCacheNotificationArgs args)
5148
{
5249
SessionLock.EnterReadLock();
5350
byte[] blob = httpContext.Session.Get(CacheId);
5451
if(blob != null)
5552
{
56-
cache.DeserializeMsalV3(blob);
53+
args.TokenCache.DeserializeMsalV3(blob);
5754
}
5855
SessionLock.ExitReadLock();
5956
}
6057

61-
public void Persist()
58+
public void Persist(TokenCacheNotificationArgs args)
6259
{
6360
SessionLock.EnterWriteLock();
6461

6562
// Reflect changes in the persistent store
66-
httpContext.Session.Set(CacheId, cache.SerializeMsalV3());
63+
httpContext.Session.Set(CacheId, args.TokenCache.SerializeMsalV3());
6764
SessionLock.ExitWriteLock();
6865
}
6966

7067
// Triggered right before MSAL needs to access the cache.
7168
// Reload the cache from the persistent store in case it changed since the last access.
7269
void BeforeAccessNotification(TokenCacheNotificationArgs args)
7370
{
74-
Load();
71+
Load(args);
7572
}
7673

7774
// Triggered right after MSAL accessed the cache.
@@ -80,7 +77,7 @@ void AfterAccessNotification(TokenCacheNotificationArgs args)
8077
// if the access operation resulted in a cache update
8178
if (args.HasStateChanged)
8279
{
83-
Persist();
80+
Persist(args);
8481
}
8582
}
8683
}

WebApp-OpenIDConnect-DotNet/Models/StaticCache.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ public class MSALStaticCache
1414
private static ReaderWriterLockSlim SessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
1515
string UserId = string.Empty;
1616
string CacheId = string.Empty;
17-
HttpContext httpContext = null;
18-
19-
ITokenCache cache;
17+
private readonly HttpContext httpContext = null;
18+
private ITokenCache cache;
2019

2120
public MSALStaticCache(string userId, HttpContext httpcontext)
2221
{
@@ -31,35 +30,34 @@ public ITokenCache EnablePersistence(ITokenCache cache)
3130
this.cache = cache;
3231
cache.SetBeforeAccess(BeforeAccessNotification);
3332
cache.SetAfterAccess(AfterAccessNotification);
34-
Load();
3533
return cache;
3634
}
3735

38-
public void Load()
36+
public void Load(TokenCacheNotificationArgs args)
3937
{
4038
SessionLock.EnterReadLock();
4139
byte[] blob = staticCache.ContainsKey(CacheId) ? staticCache[CacheId] : null ;
4240
if(blob != null)
4341
{
44-
cache.DeserializeMsalV3(blob);
42+
args.TokenCache.DeserializeMsalV3(blob);
4543
}
4644
SessionLock.ExitReadLock();
4745
}
4846

49-
public void Persist()
47+
public void Persist(TokenCacheNotificationArgs args)
5048
{
5149
SessionLock.EnterWriteLock();
5250

5351
// Reflect changes in the persistent store
54-
staticCache[CacheId] = cache.SerializeMsalV3();
52+
staticCache[CacheId] = args.TokenCache.SerializeMsalV3();
5553
SessionLock.ExitWriteLock();
5654
}
5755

5856
// Triggered right before MSAL needs to access the cache.
5957
// Reload the cache from the persistent store in case it changed since the last access.
6058
void BeforeAccessNotification(TokenCacheNotificationArgs args)
6159
{
62-
Load();
60+
Load(args);
6361
}
6462

6563
// Triggered right after MSAL accessed the cache.
@@ -68,7 +66,7 @@ void AfterAccessNotification(TokenCacheNotificationArgs args)
6866
// if the access operation resulted in a cache update
6967
if (args.HasStateChanged)
7068
{
71-
Persist();
69+
Persist(args);
7270
}
7371
}
7472
}

WebApp-OpenIDConnect-DotNet/WebApp-OpenIDConnect-DotNet.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.2</TargetFramework>
44
<RootNamespace>WebApp_OpenIDConnect_DotNet</RootNamespace>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.Identity.Client" Version="3.0.3-preview" />
7+
<PackageReference Include="Microsoft.Identity.Client" Version="4.3.0" />
88
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
99
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
1010
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="2.2.0" />

0 commit comments

Comments
 (0)