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

Jez

I'd like to manually decrypt the .AspNetCore.Identity.Application cookie that gets stored by ASP.NET Core 3.0.0 to see exactly what information it contains. I understand that Microsoft have quite significantly changed how this is done between ASP.NET Core 2.2 and 3.0, so now that 3.0 has been released to general availability, I'd like to know: how can I manually decrypt this cookie in my application code in Core 3.0?

Kahbazi

This is how you can decrypt a cookie based on CookieAuthenticationHandler

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 3.0で.AspNetCore.Identity.Application Cookieを復号化する方法は?

分類Dev

How to implement permission based authorization in ASP.net core Identity?

分類Dev

Getting Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception in .NET Core 3

分類Dev

ASP.NET Core MVC MySQL Identity

分類Dev

Customizing ASP.NET Core Identity Tables

分類Dev

Asp.net core create interface for identity

分類Dev

cookie .net Core3とAsp.netを共有する

分類Dev

ASP.Net Core2.0のタイプMicrosoft.AspNetCore.Identity.RoleManagerのサービスを解決できません

分類Dev

How to submit a file to an ASP.NET Core application

分類Dev

How to handle multiple SPA application in ASP.NET Core

分類Dev

ASP.NET Core 3:ルートプロバイダーからのスコープサービス 'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'を解決できません

分類Dev

How to remove the role-related tables from ASP.NET Identity Core 2.0

分類Dev

How do you disable HTTPS in ASP.NET Core project with Identity?

分類Dev

How do you disable HTTPS in ASP.NET Core project with Identity?

分類Dev

how to integrate ASP.NET Core 2.1 with Vue CLI 3?

分類Dev

how to integrate ASP.NET Core 2.1 with Vue CLI 3?

分類Dev

JWT Authentication for .net core 2.2 application not using Identity

分類Dev

use existing login table with asp.net core identity service

分類Dev

Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin

分類Dev

Asp.Net Core 3IdentityでカスタムSignInManagerを作成する

分類Dev

Path to LocalAppData in ASP.Net Core application

分類Dev

Localizable asp.net core + reactjs application

分類Dev

How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

分類Dev

Use cookie, authorize attribute, create session for application in .net core 2.1

分類Dev

.Net Identity - Login with forged cookie

分類Dev

ASP .NET Core - Linux nginx ASPNETCORE_ENVIROMENT エラー

分類Dev

.Net Core cookie will not be set

分類Dev

ASP.NET Identity Two Factorが機能しない-Cookieの問題?

分類Dev

How to host the basic ASP.NET Core 2 WebAPI application on IIS

Related 関連記事

  1. 1

    ASP.NET Core 3.0で.AspNetCore.Identity.Application Cookieを復号化する方法は?

  2. 2

    How to implement permission based authorization in ASP.net core Identity?

  3. 3

    Getting Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception in .NET Core 3

  4. 4

    ASP.NET Core MVC MySQL Identity

  5. 5

    Customizing ASP.NET Core Identity Tables

  6. 6

    Asp.net core create interface for identity

  7. 7

    cookie .net Core3とAsp.netを共有する

  8. 8

    ASP.Net Core2.0のタイプMicrosoft.AspNetCore.Identity.RoleManagerのサービスを解決できません

  9. 9

    How to submit a file to an ASP.NET Core application

  10. 10

    How to handle multiple SPA application in ASP.NET Core

  11. 11

    ASP.NET Core 3:ルートプロバイダーからのスコープサービス 'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'を解決できません

  12. 12

    How to remove the role-related tables from ASP.NET Identity Core 2.0

  13. 13

    How do you disable HTTPS in ASP.NET Core project with Identity?

  14. 14

    How do you disable HTTPS in ASP.NET Core project with Identity?

  15. 15

    how to integrate ASP.NET Core 2.1 with Vue CLI 3?

  16. 16

    how to integrate ASP.NET Core 2.1 with Vue CLI 3?

  17. 17

    JWT Authentication for .net core 2.2 application not using Identity

  18. 18

    use existing login table with asp.net core identity service

  19. 19

    Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin

  20. 20

    Asp.Net Core 3IdentityでカスタムSignInManagerを作成する

  21. 21

    Path to LocalAppData in ASP.Net Core application

  22. 22

    Localizable asp.net core + reactjs application

  23. 23

    How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

  24. 24

    Use cookie, authorize attribute, create session for application in .net core 2.1

  25. 25

    .Net Identity - Login with forged cookie

  26. 26

    ASP .NET Core - Linux nginx ASPNETCORE_ENVIROMENT エラー

  27. 27

    .Net Core cookie will not be set

  28. 28

    ASP.NET Identity Two Factorが機能しない-Cookieの問題?

  29. 29

    How to host the basic ASP.NET Core 2 WebAPI application on IIS

ホットタグ

アーカイブ