title: Data Protection using Entity Framework Core
body: <p>So I have followed microsoft's official guide (<a href="https://docs.microsoft.com/el-gr/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-2.2&amp;tabs=visual-studio" rel="noreferrer">https://docs.microsoft.com/el-gr/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-2.2&amp;tabs=visual-studio</a>) for encrypting data and storing them in database using Entity Framework Core, but I can't make it work accross multiple machines. So I used Entity Framework Core implementation because in the guide says "With this package, keys can be shared across multiple instances of a web app.". The app works perfectly when using it from the deployed version for example xyz.com, but It doesn't let me interfere from localhost. Will it be a problem afterwards when my virtual machine is maxed out and I want to add another one? If so how can I make it work in both the deployed site and different machines? There is no tutorial which implements that, I have searched everywhere. Thank you very much.</p>
  
  <pre><code>services.AddDataProtection()
                  .UseCryptographicAlgorithms(
                      new AuthenticatedEncryptorConfiguration()
                      {
                          EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                          ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,
  
                      }
                  ).PersistKeysToDbContext&lt;DataContext&gt;();
  </code></pre>
  
  <p>Update 12-6-2019</p>
  
  <p>So I followed microsoft's documentation (<a href="https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-encryption-at-rest?view=aspnetcore-2.2" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-encryption-at-rest?view=aspnetcore-2.2</a>)
  and it states:</p>
  
  <p>"If the app is spread across multiple machines, it may be convenient to distribute a shared X.509 certificate across the machines and configure the hosted apps to use the certificate for encryption of keys at rest"</p>
  
  <p>I generated a x.509 certificate using this tutorial:</p>
  
  <p>(<a href="https://www.youtube.com/watch?v=1xtBkukWiek" rel="noreferrer">https://www.youtube.com/watch?v=1xtBkukWiek</a>)</p>
  
  <p>My updated code:</p>
  
  <pre><code>        services.AddDataProtection()
                  .UseCryptographicAlgorithms(
                      new AuthenticatedEncryptorConfiguration()
                      {
                          EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                          ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,
  
                      }
                  )
                  // )
                  .ProtectKeysWithCertificate(new X509Certificate2("wibit-test-cert.pfx", "password"))
                  .PersistKeysToDbContext&lt;DataContext&gt;();
  
  </code></pre>
  
  <p>When testing on my local machine it works fine, but when I deploy it, I get this error:</p>
  
  <p>error: "The system cannot find the file specified"</p>
  
  <p>I have tried several ways to fix it including _hostingEnvironment.ContentRootPath or WebRootPath. Both these ways and the one I use in the updated code work in my machine but not in the deployed app.</p>
  
  <p>Any clues?</p>