Skip to content

Commit

Permalink
Update to MSAL 4.3.1 (#59)
Browse files Browse the repository at this point in the history
* Update to MSAL 4.3.1

* 431 and remove non-used cache
  • Loading branch information
bgavrilMS authored Aug 27, 2019
1 parent 1373293 commit 46516ca
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 237 deletions.
5 changes: 2 additions & 3 deletions TaskWebApp/TaskWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<HintPath>..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Identity.Client, Version=3.0.8.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Identity.Client.3.0.8\lib\net45\Microsoft.Identity.Client.dll</HintPath>
<Reference Include="Microsoft.Identity.Client, Version=4.3.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Identity.Client.4.3.1\lib\net45\Microsoft.Identity.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.JsonWebTokens, Version=5.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.JsonWebTokens.5.4.0\lib\net451\Microsoft.IdentityModel.JsonWebTokens.dll</HintPath>
Expand Down Expand Up @@ -187,7 +187,6 @@
<Compile Include="Utils\Globals.cs" />
<Compile Include="Utils\MsalAppBuilder.cs" />
<Compile Include="Utils\MSALPerUserMemoryTokenCache.cs" />
<Compile Include="Utils\MSALPerUserSessionTokenCache.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\bootstrap.css" />
Expand Down
29 changes: 9 additions & 20 deletions TaskWebApp/Utils/MSALPerUserMemoryTokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public class MSALPerUserMemoryTokenCache
/// </summary>
private readonly DateTimeOffset cacheDuration = DateTimeOffset.Now.AddHours(48);

/// <summary>
/// The internal handle to the client's instance of the Cache
/// </summary>
private ITokenCache UserTokenCache;

/// <summary>
/// Once the user signes in, this will not be null and can be ontained via a call to Thread.CurrentPrincipal
/// </summary>
Expand Down Expand Up @@ -77,18 +72,15 @@ private void Initialize(ITokenCache tokenCache, ClaimsPrincipal user)
{
this.SignedInUser = user;

this.UserTokenCache = tokenCache;
this.UserTokenCache.SetBeforeAccess(this.UserTokenCacheBeforeAccessNotification);
this.UserTokenCache.SetAfterAccess(this.UserTokenCacheAfterAccessNotification);
this.UserTokenCache.SetBeforeWrite(this.UserTokenCacheBeforeWriteNotification);
tokenCache.SetBeforeAccess(this.UserTokenCacheBeforeAccessNotification);
tokenCache.SetAfterAccess(this.UserTokenCacheAfterAccessNotification);
tokenCache.SetBeforeWrite(this.UserTokenCacheBeforeWriteNotification);

if (this.SignedInUser == null)
{
// No users signed in yet, so we return
return;
}

this.LoadUserTokenCacheFromMemory();
}

/// <summary>
Expand All @@ -107,7 +99,7 @@ internal string GetMsalAccountId()
/// <summary>
/// Loads the user token cache from memory.
/// </summary>
private void LoadUserTokenCacheFromMemory()
private void LoadUserTokenCacheFromMemory(ITokenCacheSerializer tokenCache)
{
string cacheKey = this.GetMsalAccountId();

Expand All @@ -116,21 +108,21 @@ private void LoadUserTokenCacheFromMemory()

// Ideally, methods that load and persist should be thread safe. MemoryCache.Get() is thread safe.
byte[] tokenCacheBytes = (byte[])this.memoryCache.Get(this.GetMsalAccountId());
this.UserTokenCache.DeserializeMsalV3(tokenCacheBytes);
tokenCache.DeserializeMsalV3(tokenCacheBytes);
}

/// <summary>
/// Persists the user token blob to the memoryCache.
/// </summary>
private void PersistUserTokenCache()
private void PersistUserTokenCache(ITokenCacheSerializer tokenCache)
{
string cacheKey = this.GetMsalAccountId();

if (string.IsNullOrWhiteSpace(cacheKey))
return;

// Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
this.memoryCache.Set(this.GetMsalAccountId(), this.UserTokenCache.SerializeMsalV3(), this.cacheDuration);
this.memoryCache.Set(this.GetMsalAccountId(), tokenCache.SerializeMsalV3(), this.cacheDuration);
}

/// <summary>
Expand All @@ -139,9 +131,6 @@ private void PersistUserTokenCache()
public void Clear()
{
this.memoryCache.Remove(this.GetMsalAccountId());

// Nulls the currently deserialized instance
this.LoadUserTokenCacheFromMemory();
}

/// <summary>
Expand All @@ -155,7 +144,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
// if the access operation resulted in a cache update
if (args.HasStateChanged)
{
this.PersistUserTokenCache();
this.PersistUserTokenCache(args.TokenCache);
}
}

Expand All @@ -165,7 +154,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
private void UserTokenCacheBeforeAccessNotification(TokenCacheNotificationArgs args)
{
this.LoadUserTokenCacheFromMemory();
this.LoadUserTokenCacheFromMemory(args.TokenCache);
}

/// <summary>
Expand Down
210 changes: 0 additions & 210 deletions TaskWebApp/Utils/MSALPerUserSessionTokenCache.cs

This file was deleted.

6 changes: 3 additions & 3 deletions TaskWebApp/Utils/MsalAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public static async Task ClearUserTokenCache()
//Remove the users from the MSAL's internal cache
await clientapp.RemoveAsync(account);
}
userTokenCache.Clear();

userTokenCache.Clear();
}
}
}
}
}
2 changes: 1 addition & 1 deletion TaskWebApp/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net451" />
<package id="Microsoft.Identity.Client" version="3.0.8" targetFramework="net451" />
<package id="Microsoft.Identity.Client" version="4.3.1" targetFramework="net451" />
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.4.0" targetFramework="net451" />
<package id="Microsoft.IdentityModel.Logging" version="5.4.0" targetFramework="net451" />
<package id="Microsoft.IdentityModel.Protocols" version="5.4.0" targetFramework="net451" />
Expand Down

0 comments on commit 46516ca

Please sign in to comment.