Replies: 3 comments 3 replies
-
@amcasey Please, help 🆘 |
Beta Was this translation helpful? Give feedback.
-
I haven't seen this pattern before and would be surprised to learn it worked: CancellationTokenSource.CreateLinkedTokenSource(_keyManager.GetCacheExpirationToken()).Cancel(); Generally speaking, you need to control a CancellationTokenSource in order to be able to cancel a CancellationToken. You should be able to validate in a toy console app (or just in the debugger in the real app). Having said that, resetting the cancellation token does seem like it could work - the next Protect or Unprotect call should refresh the It's definitely true that startup races are a problem for Data Protection. We've made some improvements in 9.0, but there's no perfect solution. The fundamental problem is that a given app instance doesn't know how many app instances there are, so it has no way to know whether consensus (i.e. about which is the default key) has been achieved. There were already some mitigations in place in 8.0, the main one being a grace period during which unknown keys would trigger a refresh. In practice, that retry seems to be pretty effective. Another approach you could take would be to start one instance first, let it run until it has created and published a data protection key, and then start the other instances. Obviously, this isn't always possible. |
Beta Was this translation helpful? Give feedback.
-
My (propably unsafe) method for refreshing cached key ring which is working 😃 When Protect/Unprotect throws CryptographicException (which most of the time is because key not found) then ReloadKeys is invoked.
|
Beta Was this translation helpful? Give feedback.
-
We are using IDataProtection to protect our confidential data at rest in database. Our application (NET 8) is running on two machines on Linux. When the first instance starts first time it is creating new key. A moment later the second instance starts and create own key too. We use
PersistKeysToDbContext<SecurityDbContext>
. When SecurityDbContext OnSavedChanges occurs then app sends notification to other app to reload key ring.Reloading method is like this:
To Unprotect we use the following method:
In both cases keys are not reloaded. In logs we see that ReloadedKeys contains all keys created by the first and second instance of our application but Unprotect still throws
CryptographicException
with message that key not found even though theGetAllKeys
method shows that the given key is loaded.Maybe our reloading code is wrong. So,,,,
How to properly reload keys so that the key ring can see and use them?
Please help...
PS
Restarting instance (which can not see reloaded keys) helps but that is not a solution at all. 😞
Beta Was this translation helpful? Give feedback.
All reactions