While trying to use DangerousUnprotect(), I am getting above mentioned error message. Protect() & Unprotect is working fine. I am not able to understand what is causing this to throw said exception.
Can someone please advise me on what's wrong with data protection provider?
Package : Microsoft.AspNetCore.DataProtection.Extensions 2.1.1
class Program
{
public static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp-keys"))
.ProtectKeysWithDpapi();
var services = serviceCollection.BuildServiceProvider();
var instance = ActivatorUtilities.CreateInstance<Implementation>(services, services.GetService<IKeyManager>());
var protectedPayload = instance.encrypt("test");
var unprotectedPayload = instance.decrypt(protectedPayload);
instance.revokeAllKeys();
var dangerouslyUnprotectedPayload = instance.dangerousDecrypt(protectedPayload);
Console.ReadKey();
}
}
public class Implementation
{
private IDataProtector _pro;
private IKeyManager _keymgr;
public Implementation(IDataProtectionProvider provider, IKeyManager manager)
{
this._pro = provider.CreateProtector("purpose");
this._keymgr = manager;
}
public string encrypt(string plain)
{
return this._pro.Protect(plain);
}
public string decrypt(string encrypted)
{
return this._pro.Unprotect(encrypted);
}
public string dangerousDecrypt(string encryptedWithRevokedKey)
{
var persistentProtector = this._pro as IPersistedDataProtector;
**// Throws exception at this execution
var plain = persistentProtector.DangerousUnprotect(Encoding.UTF8.GetBytes(encryptedWithRevokedKey), true, out var migrate, out var revoked);**
return Encoding.UTF8.GetString(plain);
}
public void revokeAllKeys()
{
this._keymgr.RevokeAllKeys(DateTimeOffset.Now);
}
}
While trying to use DangerousUnprotect(), I am getting above mentioned error message. Protect() & Unprotect is working fine. I am not able to understand what is causing this to throw said exception.
Can someone please advise me on what's wrong with data protection provider?
Package : Microsoft.AspNetCore.DataProtection.Extensions 2.1.1