title: Adding a SessionStore (ITicketStore) to my application cookie makes my Data Protection Provider fail to work
body: <p>tl;dr</p>
  
  <ul>
  <li>Have .NET Core 2.0 application which uses a Data Protection Provider which persists a key file across all of the sites on my domain.</li>
  <li>Worked fine, however, application cookie became too big.</li>
  <li>Implemented a SessionStore on the cookie using ITicketStore</li>
  <li>Cookie size is greatly reduced, however, the key from the DPP no longer persists across my sites.</li>
  </ul>
  
  <p>Is there something I'm supposed to do in my ITicketStore implementation to fix this? I'm assuming so, since this is where the problem arises, however, I could not figure it out.</p>
  
  <p>Some snippets:</p>
  
  <hr>
  
  <p><strong>Startup.cs --> ConfigureServices()</strong>        </p>
  
  <pre><code>var keysFolder = $@"c:\temp\_WebAppKeys\{_env.EnvironmentName.ToLower()}";
  var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(keysFolder));
  var dataProtector = protectionProvider.CreateProtector(
              "Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware",
              "Cookies",
              "v2");
  
  --snip--
  
  services.AddSingleton&lt;ITicketStore, TicketStore&gt;();
  
  --snip--
  
  services.AddDataProtection()
      .PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
      .SetApplicationName("app_auth");
  
  services.ConfigureApplicationCookie(options =&gt;
  {
      options.Cookie.Name = ".XAUTH";
      options.Cookie.Domain = ".domain.com";
      options.ExpireTimeSpan = TimeSpan.FromDays(7);
      options.LoginPath = "/Account/Login";
      options.DataProtectionProvider = protectionProvider;
      options.TicketDataFormat = new TicketDataFormat(dataProtector);
      options.CookieManager = new ChunkingCookieManager();
      options.SessionStore = services.BuildServiceProvider().GetService&lt;ITicketStore&gt;();
  });
  </code></pre>
  
  <hr>
  
  <p><strong>TicketStore.cs</strong></p>
  
  <pre><code>public class TicketStore : ITicketStore
  {
      private IMemoryCache _cache;
      private const string KeyPrefix = "AuthSessionStore-";
  
  public TicketStore(IMemoryCache cache)
  {
      _cache = cache;
  }
  
  public Task RemoveAsync(string key)
  {
      _cache.Remove(key);
      return Task.FromResult(0);
  }
  
  public Task RenewAsync(string key, AuthenticationTicket ticket)
  {
      var options = new MemoryCacheEntryOptions
      {
          Priority = CacheItemPriority.NeverRemove
      };
      var expiresUtc = ticket.Properties.ExpiresUtc;
  
      if (expiresUtc.HasValue)
      {
          options.SetAbsoluteExpiration(expiresUtc.Value);
      }
  
      options.SetSlidingExpiration(TimeSpan.FromMinutes(60));
  
      _cache.Set(key, ticket, options);
  
      return Task.FromResult(0);
  }
  
  public Task&lt;AuthenticationTicket&gt; RetrieveAsync(string key)
  {
      AuthenticationTicket ticket;
      _cache.TryGetValue(key, out ticket);
      return Task.FromResult(ticket);
  }
  
  public async Task&lt;string&gt; StoreAsync(AuthenticationTicket ticket)
  {
      var key = KeyPrefix + Guid.NewGuid();
      await RenewAsync(key, ticket);
      return key;
  }
  </code></pre>