From 9c532ba62321da739485a20c9be508e6eae65df6 Mon Sep 17 00:00:00 2001 From: jennyf19 Date: Mon, 29 Jun 2020 07:52:10 -0700 Subject: [PATCH] fix bugs in session cache provider. (#265) - remove static - remove commit, as it's done automatically --- .../Session/MsalSessionTokenCacheProvider.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Identity.Web/TokenCacheProviders/Session/MsalSessionTokenCacheProvider.cs b/src/Microsoft.Identity.Web/TokenCacheProviders/Session/MsalSessionTokenCacheProvider.cs index 06cc6aaa7..0120ec746 100644 --- a/src/Microsoft.Identity.Web/TokenCacheProviders/Session/MsalSessionTokenCacheProvider.cs +++ b/src/Microsoft.Identity.Web/TokenCacheProviders/Session/MsalSessionTokenCacheProvider.cs @@ -13,7 +13,7 @@ namespace Microsoft.Identity.Web.TokenCacheProviders.Session /// An implementation of token cache for confidential clients backed by an HTTP session. /// /// - /// 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 public void ConfigureServices(IServiceCollection services) in Startup.cs, add the following: @@ -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; /// /// MSAL Token cache provider constructor. /// /// Configuration options. - /// accessor for an HttpContext. + /// Accessor for an HttpContext. /// Logger. public MsalSessionTokenCacheProvider( IOptions microsoftIdentityOptions, @@ -59,7 +59,7 @@ protected override async Task ReadCacheBytesAsync(string cacheKey) { await CurrentHttpContext.Session.LoadAsync().ConfigureAwait(false); - s_sessionLock.EnterReadLock(); + _sessionLock.EnterReadLock(); try { if (CurrentHttpContext.Session.TryGetValue(cacheKey, out byte[] blob)) @@ -75,53 +75,53 @@ protected override async Task ReadCacheBytesAsync(string cacheKey) } finally { - s_sessionLock.ExitReadLock(); + _sessionLock.ExitReadLock(); } } /// /// Writes the token cache identified by its key to the serialization mechanism. /// - /// key for the cache (account ID or app ID). - /// blob to write to the cache. + /// Key for the cache (account ID or app ID). + /// Blob to write to the cache. 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(); } } /// - /// Removes a cache described from its key. + /// Removes a cache described by its key. /// - /// key of the token cache (user account or app ID). + /// Key of the token cache (user account or app ID). 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); } }