如何在 ASP.NET Core 3.0 中解密 .AspNetCore.Identity.Application cookie?

How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

我想手动解密 ASP.NET Core 3.0.0 存储的 .AspNetCore.Identity.Application cookie,以查看它包含的确切信息。我知道 Microsoft 在 ASP.NET Core 2.2 和 3.0 之间做了相当大的改变,所以现在 3.0 已经公开发布,我想知道:我如何手动解密我的 cookie Core 3.0 中的应用程序代码?

这是根据CookieAuthenticationHandler

解密cookie的方法
public class Startup
{
    private CookieAuthenticationOptions _storedOption;


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication()
            .AddCookie(option =>
            {
                _storedOption = option;
            });
    }

    public AuthenticationTicket Decrypt(HttpContext context, string cookie)
    {
        AuthenticationTicket ticket = _storedOption.TicketDataFormat.Unprotect(cookie, GetTlsTokenBinding(context));
        return ticket;
    }

    public string DecryptRaw(HttpContext context, string cookie)
    {
        IDataProtectionProvider dataProtectionProvider = _storedOption.DataProtectionProvider;

        IDataProtector protector = dataProtectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Identity.Application", "v2");

        string purpose = GetTlsTokenBinding(context);

        if (!string.IsNullOrEmpty(purpose))
        {
            protector = protector.CreateProtector(purpose);
        }

        var protectedData = Base64UrlTextEncoder.Decode(cookie);

        byte[] userData = protector.Unprotect(protectedData);

        var rawText = Encoding.UTF8.GetString(userData);

        return rawText;
    }

    private string GetTlsTokenBinding(HttpContext context)
    {
        var binding = context.Features.Get<ITlsTokenBindingFeature>()?.GetProvidedTokenBindingId();
        return binding == null ? null : Convert.ToBase64String(binding);
    }
}