Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Security.Cryptography.CryptographicException: 'The payload was invalid. For more information go to http://aka.ms/dataprotectionwarning' #39958

Closed
1 task done
palmej2 opened this issue Feb 3, 2022 · 12 comments
Labels
area-dataprotection Includes: DataProtection

Comments

@palmej2
Copy link

palmej2 commented Feb 3, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

There is a bug in the usage of the Microsoft.AspNetCore.DataProtection.SystemWeb nuget package.

I have a basic ASP.NET MVC .NET 4.8 project created using the examples in the article with the DataProtectionDemo.cs and using the Microsoft.AspNetCore.DataProtection.SystemWeb nuget package. I did everything exactly how the article described. Setup as follows:

image

-I created a basic .NET Core Console application injecting the IDataProtectionProvider and setting up the AddDataProjection() exactly how it was done in the first example (but in core).

image

If I protect a value (any value) on the ASP.NET site and try and unprotect that value on the console application I get the "The payload was invalid.". Either there missing information in that product article critical to getting this working, or there is a bug in the process. I've double-checked everything the article mentions.

keys are generated correctly in the same file location for both applications
application name is exactly the same in both applications (my-app)

I have generated a github repo reproducing the error here https://github.com/palmej2/DNETFW-2-DNETCORE

NOTE: This is issue originated from dotnet/AspNetCore.Docs#24847 and was directed to make bug

Expected Behavior

Anything encrypted in .NET FW 4.8 should be able to be decrypted and read in .NET as long as the same security keys are used in both applications.

Steps To Reproduce

Get https://github.com/palmej2/DNETFW-2-DNETCORE

Instructions to recreate issues found:

  1. The default folder is for the keys is C:\test\myapp-keys. If you want to change it update the code in consoleapp1/program.cs & WebApplication1/DataProtectionDemo.cs.
  2. Run the WebApplication project and copy the decrypted value that it produces.
  3. Run the ConsoleApp1 project and paste that decrypted value into it.

Currently #3 is failing, and it shouldn't fail. It should decrypt the value correctly, which currently should be "TEST VALUE".

Exceptions (if any)

System.Security.Cryptography.CryptographicException: 'The payload was invalid. For more information go to http://aka.ms/dataprotectionwarning'

.NET Version

4.8/6.0

Anything else?

No response

@blowdart
Copy link
Contributor

blowdart commented Feb 4, 2022

So, to mirror what I said in docs, the package was primarily meant for auth cookies, as a stop gap to help people migrate. This just reminds me to draw a line under it and mark it as unsupported and deprecated.

What's the real-world scenario you're trying to solve here?

@palmej2
Copy link
Author

palmej2 commented Feb 5, 2022

I work in the financial industry, and lot of things in a sessions need to be encrypted - some data at rest as well. So we've used it a lot of just general encryption/decryption and data protection of the application. It's maybe not the best solution for the problem but it's been used now for almost 8 years and it works. The project currently is built in .NET FW 4.8. Recently I've been trying to figure out a pathway to migrate (slowly and safely) to .NET Core. Because the size of this project and how many teams I have working on it, the only realistic way to upgrade would be to build out a .NET Core project in parallel and move one API at a time. However, by doing that I need encryption to work the same in both applications. Do we use cookies? Yeah. But we just keep a SessionID in the cookie that corresponds to the session data on the server-side. Do we use view state? Not that I'm aware of. Eventually I'm going to convert all the data at rest that needs encryption into Key Vault. However, I'd still use DataProjection because there still is transient data during a session that, at times, needs to be encrypted - not a lot, but some. So this nuget package got me excited because it looked like a promising way for me to have the same Data Protection for both my apps (4.8, Core) while running them at the same time - but it doesn't work currently.

@adityamandaleeka
Copy link
Member

Related: #40083

We'll probably see similar cases as we work on the migration epic in 7.

@adityamandaleeka adityamandaleeka added this to the .NET 7 Planning milestone Feb 11, 2022
@ghost
Copy link

ghost commented Feb 11, 2022

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ljohnson91
Copy link

Dealing with this issue as well. This issue is a huge blocking problem and I find it very frustrating that it does not work.

@iso8859
Copy link

iso8859 commented Apr 27, 2022

I just got this problem because I changed Kestrel initialisation to be able to use it from Windows service.

.NET 6 project. I moved from

CreateHostBuilder(args).Build().Run();

...

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseIISIntegration();
        });        

to

...
    // In RunTask service start method
    await _webApp.StartAsync(m_exit.Token);
...

public static void Main(string[] args)
{
    // Topshelf service
    var rc = HostFactory.Run(x =>
    {
        x.Service<RunTask>(hostSettings => new RunTask(CreateWebApp(args)));
        x.SetDescription("xxx");
        x.SetDisplayName("xxx");
        x.SetServiceName("xxx");
    });
}

public static WebApplication CreateWebApp(string[] args)
{
    WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
    builder.Host.ConfigureDefaults(args);
    builder.WebHost.UseIISIntegration();
    Startup s = new Startup(builder.Configuration, builder.Environment);
    s.ConfigureServices(builder.Services);
    var app = builder.Build();
    s.Configure(app, app.Environment);
    return app;
}

For my case the workaround was easy because I don't need to keep my protected storage.

ProtectedBrowserStorageResult<string> result = default;
try
{
     result = await m_protectedLocalStore.GetAsync<string>("sr");
}
catch(CryptographicException)
{
    await m_protectedLocalStore.DeleteAsync("sr");
}
if (result.Success)
{
    sessionRequest = result.Value;
}

The question is why protected storage is sensitive to my Kestrel init ?

@shahedbd
Copy link

shahedbd commented Oct 5, 2022

using Microsoft.AspNetCore.DataProtection;


services.AddDataProtection()
            .SetApplicationName("ProjectName")
            .AddKeyManagementOptions(options =>
            {
                options.NewKeyLifetime = new TimeSpan(180, 0, 0, 0);
                options.AutoGenerateKeys = true;
            });

@adityamandaleeka
Copy link
Member

@wtgodbe Reassigning this to you to follow up.

@ghost
Copy link

ghost commented Oct 5, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@wtgodbe wtgodbe removed their assignment Nov 28, 2022
@ghost
Copy link

ghost commented Jan 18, 2023

So what's going on here? This exception has been happening since day 1 for me, requiring customers to clear their local storage. Is there a recommended way for SSB to set local storage values other than this?

@jamesjeongolo
Copy link

I'm wondering if there's been any movement on this issue. My team is currently migrating our net48 app to netcore. We plan having the netcore app and the net48 app running as a fallback. We'd want to be authenticate our servers for both apps. We're persisting our keys in an xml repository for key management. All of the configurations are identical but we're running in this issue as well.

@claudiaregio
Copy link

Closing as out of scope/unsupported use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection
Projects
None yet
Development

No branches or pull requests

10 participants