Based on what I learned from How to manually decrypt and asp.net core auth cookie I tried to do the similar. The only difference is that the cookie is set by asp.net core identity. Here is the piece in SetUp.cs:
services.AddIdentity<ApplicationUser, IdentityRole>( options =>
{
options.Cookies.ApplicationCookie.AuthenticationScheme = "Cookies";
options.Cookies.ApplicationCookie.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"));
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
And here is how I tried to setup decrypt in homeController.cs:
var cookies = HttpContext.Request.Cookies;
var provider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"));
//Get a data protector to use with either approach
var dataProtector = provider.CreateProtector("Identity.Application", "Cookies", "v2");
//Get the decrypted cookie as plain text
UTF8Encoding specialUtf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
foreach (var cookie in cookies)
{
byte[] protectedBytes = Base64UrlTextEncoder.Decode(cookie.Value);
byte[] plainBytes = dataProtector.Unprotect(protectedBytes);
string plainText = specialUtf8Encoding.GetString(plainBytes);
}
But I always get the following error: The key {******-****} was not found in the key ring.
How to match up the key/dataProtectionProvider?