Skip to content

Commit

Permalink
fix bugs in session cache provider. (#265)
Browse files Browse the repository at this point in the history
- remove static
- remove commit, as it's done automatically
  • Loading branch information
jennyf19 authored Jun 29, 2020
1 parent f2bc3a7 commit 9c532ba
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Identity.Web.TokenCacheProviders.Session
/// An implementation of token cache for confidential clients backed by an HTTP session.
/// </summary>
/// <remarks>
/// For this session cache to work effectively the ASP.NET Core session has to be configured properly.
/// For this session cache to work effectively, the ASP.NET Core session has to be configured properly.
/// The latest guidance is provided at https://docs.microsoft.com/aspnet/core/fundamentals/app-state
///
/// In the method <c>public void ConfigureServices(IServiceCollection services)</c> in Startup.cs, add the following:
Expand All @@ -32,13 +32,13 @@ namespace Microsoft.Identity.Web.TokenCacheProviders.Session
public class MsalSessionTokenCacheProvider : MsalAbstractTokenCacheProvider, IMsalTokenCacheProvider
{
private HttpContext CurrentHttpContext => _httpContextAccessor.HttpContext;
private ILogger _logger;
private readonly ILogger _logger;

/// <summary>
/// MSAL Token cache provider constructor.
/// </summary>
/// <param name="microsoftIdentityOptions">Configuration options.</param>
/// <param name="httpContextAccessor">accessor for an HttpContext.</param>
/// <param name="httpContextAccessor">Accessor for an HttpContext.</param>
/// <param name="logger">Logger.</param>
public MsalSessionTokenCacheProvider(
IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
Expand All @@ -59,7 +59,7 @@ protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey)
{
await CurrentHttpContext.Session.LoadAsync().ConfigureAwait(false);

s_sessionLock.EnterReadLock();
_sessionLock.EnterReadLock();
try
{
if (CurrentHttpContext.Session.TryGetValue(cacheKey, out byte[] blob))
Expand All @@ -75,53 +75,53 @@ protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey)
}
finally
{
s_sessionLock.ExitReadLock();
_sessionLock.ExitReadLock();
}
}

/// <summary>
/// Writes the token cache identified by its key to the serialization mechanism.
/// </summary>
/// <param name="cacheKey">key for the cache (account ID or app ID).</param>
/// <param name="bytes">blob to write to the cache.</param>
/// <param name="cacheKey">Key for the cache (account ID or app ID).</param>
/// <param name="bytes">Blob to write to the cache.</param>
protected override async Task WriteCacheBytesAsync(string cacheKey, byte[] bytes)
{
s_sessionLock.EnterWriteLock();
_sessionLock.EnterWriteLock();
try
{
_logger.LogInformation($"Serializing session {CurrentHttpContext.Session.Id}, cacheId {cacheKey}");

// Reflect changes in the persistent store
CurrentHttpContext.Session.Set(cacheKey, bytes);
await CurrentHttpContext.Session.CommitAsync().ConfigureAwait(false);
await Task.CompletedTask.ConfigureAwait(false);
}
finally
{
s_sessionLock.ExitWriteLock();
_sessionLock.ExitWriteLock();
}
}

/// <summary>
/// Removes a cache described from its key.
/// Removes a cache described by its key.
/// </summary>
/// <param name="cacheKey">key of the token cache (user account or app ID).</param>
/// <param name="cacheKey">Key of the token cache (user account or app ID).</param>
protected override async Task RemoveKeyAsync(string cacheKey)
{
s_sessionLock.EnterWriteLock();
_sessionLock.EnterWriteLock();
try
{
_logger.LogInformation($"Clearing session {CurrentHttpContext.Session.Id}, cacheId {cacheKey}");

// Reflect changes in the persistent store
CurrentHttpContext.Session.Remove(cacheKey);
await CurrentHttpContext.Session.CommitAsync().ConfigureAwait(false);
await Task.CompletedTask.ConfigureAwait(false);
}
finally
{
s_sessionLock.ExitWriteLock();
_sessionLock.ExitWriteLock();
}
}

private static readonly ReaderWriterLockSlim s_sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private readonly ReaderWriterLockSlim _sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}
}

0 comments on commit 9c532ba

Please sign in to comment.