基于我从How to manually decrypt and asp.net core auth cookie学到的知识 我试着做类似的事情。唯一的区别是cookie是由asp.net核心身份设置的。这是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();
以下是我在homeController.cs中尝试设置decrypt的方法:
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);
}
但我总是收到以下错误: 钥匙圈中找不到钥匙{****** - ****}。
如何匹配key / dataProtectionProvider?