title: ASP.Net Core web-farm with distributed session handling in Redis
body: <p>I'm trying to create a web-farm with multiple docker containers all running the same ASP.NET Core  3.1 app. Hence, I need to manage a distributed session and my idea was to use Redis to do that.</p>
  <p>According to <a href="https://medium.com/swlh/how-to-distribute-data-protection-keys-with-an-asp-net-core-web-app-8b2b5d52851b" rel="nofollow noreferrer">this article</a> I added the following statements to my <code>ConfigureServices</code> method:</p>
  <pre><code>var redis = ConnectionMultiplexer.Connect(redis_connection_string);
  services.AddDataProtection()
    .PersistKeysToStackExchangeRedis(redis, &quot;DataProtection-Keys&quot;)
    .SetApplicationName(&quot;seminarmanager&quot;);
  
  services.AddStackExchangeRedisCache(o =&gt;
  {
    o.Configuration = redis_connection_string;
    o.InstanceName = &quot;seminarmanager&quot;;
  });
  </code></pre>
  <p>Of course, I added two packages from Nuget to my app:</p>
  <ul>
  <li><code>Microsoft.AspNetCore.DataProtection.StackExchangeRedis</code></li>
  <li><code>Microsoft.Extensions.Caching.StackExchangeRedis</code></li>
  </ul>
  <p>Unfortunately, distributed session handling does not work and I get a bunch of errors like the following:</p>
  <blockquote>
  <p>System.Security.Cryptography.CryptographicException: The key {9b51134a-a57a-4129-9441-72859a985ab0} was not found in the key ring.</p>
  <p>at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus&amp; status)<br />
  at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean&amp; requiresMigration, Boolean&amp; wasRevoked)<br />
  at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)<br />
  at Microsoft.AspNetCore.Session.CookieProtection.Unprotect(IDataProtector protector, String protectedText, ILogger logger)</p>
  <p>Microsoft.AspNetCore.Session.SessionMiddleware: Warning: Error unprotecting the session cookie.</p>
  </blockquote>
  <p>In Redis the key &quot;DataProtection-Keys&quot; was created but has not value.</p>
  <p>How can I successfully configure distributed session/key management with Redis?</p>
  <p>Best regards...</p>