Key not valid for use in specified state

I am receiving the following error, any idea what is going on?

A fatal exception occurred
Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor ‘Void .ctor(Octopus.Platform.Deployment.Configuration.IKeyValueStore, Octopus.Platform.Deployment.Configuration.ICommunicationsConfiguration, Octopus.Shared.Security.ICertificateGenerator)’ on type ‘TentacleConfiguration’. —> Key not valid for use in specified state.
(See inner exception for details.) —> System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
at Octopus.Shared.Configuration.AbstractKeyValueStore.Get[TData](String name, TData defaultValue, Nullable1 protectionScope) in c:\TeamCity\buildAgent\work\1116bd9da9e239fd\source\Octopus.Shared\Configuration\AbstractKeyValueStore.cs:line 37 at Octopus.Shared.Configuration.TentacleConfiguration..ctor(IKeyValueStore settings, ICommunicationsConfiguration communicationsConfiguration, ICertificateGenerator certificateGenerator) in c:\TeamCity\buildAgent\work\1116bd9da9e239fd\source\Octopus.Shared\Configuration\TentacleConfiguration.cs:line 27 at lambda_method(Closure , Object[] ) at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() --- End of inner exception stack trace --- at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters)
at Autofac.Features.LazyDependencies.LazyRegistrationSource.<>c__DisplayClass51.<>c__DisplayClass7.<CreateLazyRegistration>b__4() at System.Lazy1.CreateValue()
at System.Lazy1.LazyInitValue() at Octopus.Tentacle.Commands.NewCertificateCommand.Start() in c:\TeamCity\buildAgent\work\1116bd9da9e239fd\source\Octopus.Tentacle\Commands\NewCertificateCommand.cs:line 34 at Octopus.Shared.Startup.ConsoleHost.Run(Action1 start, Action shutdown) in c:\TeamCity\buildAgent\work\1116bd9da9e239fd\source\Octopus.Shared\Startup\ConsoleHost.cs:line 36

Hi Nick,

I’d guess that the problem here is that Tentacle.exe new-certificate is being run remotely, in a session without the user profile loaded. If so you probably need to use the export/import certificate support detailed in:

Other causes to investigate would possibly include whether the user running the command has sufficient permission on the machine to use DPAPI, but I’d guess you’re running it with admin rights so I’d rule that out.

Any further clues?

Hi Nicholas,

I actually figured out the issue. This comes from the WebFarm project
running on Azure Web Role that you recommended to me a month or so ago.
The problem is every time Microsoft restarts the role or a new deployment
happens, there is about a 50/50 chance that I see this error. I tracked it
back to something in the .config file, so I just added a line that OnStart
it deletes the .config file and that has seemed to work thus far.


Great, thanks for the update Nick.

FYI this problem is fixed in the latest version of the AzureWebFarm.OctopusDeploy project assuming that’s what you are talking about.

I just got this error while installing on a brand new Windows 2012 R2 instance on EC2.

I installed the x86 version of Tentacle instead and it worked. Seems there’s a bug in the x64 version…

I had this error installing a ‘fresh’ Tentacle on a rebuilt server that still had the old data drive attached (with an old Tentacle install, which I was attempting to go over the top of).
When I blew away the old D:\Octopus\Tentacle directory it all worked fine.