Data Protection in .net core 3.1 resorting to default in Docker

  asp.net-core, asp.net-core-3.1, docker

I am trying to implement Data Protection in my .net core application. The implementation is pretty straightforward, use IXmlRepository and store the Keys in Postgres DB. This works fine when I run it on my local system, the key gets stored in the DB and with every request it gets checked.

The problem surfaces when I trying to run it in Docker. The key still gets stored in the DB but as soon as I launch the application it throws this error

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.IO.DirectoryNotFoundException: C:Applocale

From the looks of it, it seems like the application is not looking for the key in the DB but in C:Applocale and I am not sure why the implementation resorts to the default implementation PersistKeysToFileSystem. Below is the code snippet

Startup.cs

 public void ConfigureServices(IServiceCollection services)
 {
   ... other services.....
   services.ConfigureDataProtectionService();
 }

ServiceExtension.cs

 public static class DataProtectionsServiceExtension
    {
        public static void ConfigureDataProtectionService(this IServiceCollection services)
        {
            services.ConfigureDataProtection();
        }

        private static void ConfigureDataProtection(this IServiceCollection services)
        {
            var builder = services.AddDataProtection();

            builder.Services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(
                serviceProvider =>
                {
                    var xmlRepository = serviceProvider.Resolve<IXmlRepository>();

                    return new ConfigureOptions<KeyManagementOptions>(options => options.XmlRepository = xmlRepository);
                });
        }
    }

Could anyone help me with this. Thanks

Source: Docker Questions

LEAVE A COMMENT