Reputation: 476
net5.0
ASP.NET Core app is deployed on Azure App Service. It is using https://a.free.xxxx.ravendb.cloud
with certificates. The certificate is loading correctly in my local environment. It is giving me the error below; however, the certificate is also added in Azure Portal - TLS/SSL settings
.
Unhandled exception. System.InvalidOperationException: The supplied CN=free.transfocus certificate contains no private key. Constructing the certificate with the 'X509KeyStorageFlags.MachineKeySet' flag may solve this problem.
at Raven.Client.Documents.DocumentStore.Initialize() in C:\Builds\RavenDB-Stable-5.1\51016\src\Raven.Client\Documents\DocumentStore.cs:line 222
at Raven.DependencyInjection.RavenOptionsSetup.GetDocumentStore(Action`1 configureDbStore)
at Raven.DependencyInjection.ServiceCollectionExtensions.<>c.<AddRavenDbDocStore>b__1_0(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Transfocus.Middleware.Program.Main(String[] args) in D:\a\1\s\Transfocus.Middleware\Transfocus.Middleware\Program.cs:line 26
Program.cs
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var store = services.GetRequiredService<IDocumentStore>();
var expiration = TimeSpan.FromDays(90);
var errorExpiration = TimeSpan.FromDays(180);
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.With()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().ToLower().Contains("hangfire")))
.Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().ToLower().Contains(".ico")) || c.Properties.Any(p => p.Value.ToString().ToLower().Contains(".png")) || c.Properties.Any(p => p.Value.ToString().ToLower().Contains(".jpg")) )
.WriteTo.RavenDB(store, expiration: expiration, errorExpiration: errorExpiration)
.CreateLogger();
}
My Startup class is like below
var dbConfig = Configuration.GetSection("Database").Get<AppSettings.Database>();
var store = new DocumentStore
{
Urls = dbConfig.Urls,
Database = dbConfig.DatabaseName
};
store.Conventions.UseOptimisticConcurrency = true;
if (!string.IsNullOrWhiteSpace(dbConfig.CertPath))
store.Certificate = new X509Certificate2(dbConfig.CertPath, dbConfig.CertPass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
store.Initialize();
services.AddSingleton<IDocumentStore>(store);
IndexCreation.CreateIndexes(typeof(Startup).Assembly, store);
services.AddScoped<IAsyncDocumentSession>(sp => sp.GetService<IDocumentStore>()?.OpenAsyncSession());
services.AddScoped<IDocumentSession>(sp => sp.GetService<IDocumentStore>()?.OpenSession());
Upvotes: 2
Views: 339
Reputation: 21883
Thanks for bartonjs's answer, it could be useful to you.
What is the impact of the PersistKeySet
-StorageFlag when importing a Certificate in C#
Reference links:
1. Install a PFX file by using X509Certificate from a standard .NET application
2. Add support for opening a PFX with an ephemeral key #17166
Upvotes: 1