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

耶兹

我想手动解密.AspNetCore.Identity.Application由ASP.NET Core 3.0.0存储cookie,以查看其包含的确切信息。我了解微软已经相当大地改变了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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ASP.NET Core Identity 3 Cookie超时

来自分类Dev

如何更改“.AspNetCore.Identity.Application”cookie 过期时间?

来自分类Dev

如何在ASP.NET Core中设置cookie validateInterval?

来自分类Dev

ASP.NET Core 3:无法从根提供程序解析作用域服务'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'

来自分类Dev

ASP.NET Identity 2.0解密Owin Cookie

来自分类Dev

如何更改.Net Core 3.1 Identity Server 4中的持久性Cookie过期时间

来自分类Dev

在Application_EndRequest阶段之后,Owin如何设置Asp.Net Identity身份验证cookie?

来自分类Dev

如何将Asp.Net Identity中的大Cookie分成较小的Cookie,以允许很多索赔?

来自分类Dev

如何在ASP.NET Core Identity中扩展和验证会话?

来自分类Dev

如何在ASP.NET CORE MVC中覆盖默认的Identity AccessDenied路由

来自分类Dev

共享cookie .net Core 3和Asp.net

来自分类Dev

如何在 ASP.NET Core 中设置永不过期的 cookie?

来自分类Dev

如何在Asp.net Core MVC(又名Asp.Net 5 RC1)中检查响应cookie?

来自分类Dev

使用Microsoft.AspNetCore.Rewrite扩展对ASP.NET Core 3中的静态文件进行URL重写

来自分类Dev

在Identity Asp.net core 3 MVC中创建服务IUserStore时出错

来自分类Dev

Asp.Net核心MVC6如何在Identity 3中初始添加角色

来自分类Dev

如何在ASP.NET 5 Identity中设置PasswordHasherCompatibilityMode.IdentityV3?

来自分类Dev

如何在ASP.NET中加密和解密Cookie

来自分类Dev

如何访问cookie asp.net core

来自分类Dev

如何在 ASP.NET Core Identity 中控制器的构造函数中获取登录用户名

来自分类Dev

如何在Asp.Net Core Identity中获取字体真棒图标以显示在帐户管理页面上?

来自分类Dev

Identity如何在具有ASP.NET Core MVC 3.1的单个用户帐户的项目模板中工作?

来自分类Dev

如何在ASP.NET Core 2.2中使用MVC代替剃须刀页面来使用Identity

来自分类Dev

ASP.NET Identity会话cookie的安全性如何?

来自分类Dev

在.NET Core 3中获取Microsoft.AspNetCore.Hosting.Diagnostics [6]应用程序启动异常

来自分类Dev

替换Cookie ASP.NET Core 1.0中的值

来自分类Dev

ASP.NET Core rc2中的Cookie

来自分类Dev

更改ASP.NET Core MVC中的默认cookie名称

来自分类Dev

使用身份在Asp.Net Core 3.1中配置Cookie

Related 相关文章

  1. 1

    ASP.NET Core Identity 3 Cookie超时

  2. 2

    如何更改“.AspNetCore.Identity.Application”cookie 过期时间?

  3. 3

    如何在ASP.NET Core中设置cookie validateInterval?

  4. 4

    ASP.NET Core 3:无法从根提供程序解析作用域服务'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'

  5. 5

    ASP.NET Identity 2.0解密Owin Cookie

  6. 6

    如何更改.Net Core 3.1 Identity Server 4中的持久性Cookie过期时间

  7. 7

    在Application_EndRequest阶段之后,Owin如何设置Asp.Net Identity身份验证cookie?

  8. 8

    如何将Asp.Net Identity中的大Cookie分成较小的Cookie,以允许很多索赔?

  9. 9

    如何在ASP.NET Core Identity中扩展和验证会话?

  10. 10

    如何在ASP.NET CORE MVC中覆盖默认的Identity AccessDenied路由

  11. 11

    共享cookie .net Core 3和Asp.net

  12. 12

    如何在 ASP.NET Core 中设置永不过期的 cookie?

  13. 13

    如何在Asp.net Core MVC(又名Asp.Net 5 RC1)中检查响应cookie?

  14. 14

    使用Microsoft.AspNetCore.Rewrite扩展对ASP.NET Core 3中的静态文件进行URL重写

  15. 15

    在Identity Asp.net core 3 MVC中创建服务IUserStore时出错

  16. 16

    Asp.Net核心MVC6如何在Identity 3中初始添加角色

  17. 17

    如何在ASP.NET 5 Identity中设置PasswordHasherCompatibilityMode.IdentityV3?

  18. 18

    如何在ASP.NET中加密和解密Cookie

  19. 19

    如何访问cookie asp.net core

  20. 20

    如何在 ASP.NET Core Identity 中控制器的构造函数中获取登录用户名

  21. 21

    如何在Asp.Net Core Identity中获取字体真棒图标以显示在帐户管理页面上?

  22. 22

    Identity如何在具有ASP.NET Core MVC 3.1的单个用户帐户的项目模板中工作?

  23. 23

    如何在ASP.NET Core 2.2中使用MVC代替剃须刀页面来使用Identity

  24. 24

    ASP.NET Identity会话cookie的安全性如何?

  25. 25

    在.NET Core 3中获取Microsoft.AspNetCore.Hosting.Diagnostics [6]应用程序启动异常

  26. 26

    替换Cookie ASP.NET Core 1.0中的值

  27. 27

    ASP.NET Core rc2中的Cookie

  28. 28

    更改ASP.NET Core MVC中的默认cookie名称

  29. 29

    使用身份在Asp.Net Core 3.1中配置Cookie

热门标签

归档