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

The antiforgery token could not be decrypted: The payload was invalid #47774

Closed
1 task done
ptzremote opened this issue Apr 19, 2023 · 5 comments
Closed
1 task done
Labels
area-dataprotection Includes: DataProtection

Comments

@ptzremote
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I want to send a request from one application to another and validate the anti-forgery token.

Expected Behavior

Validation will be complete successfully.

Steps To Reproduce

  • Create ASP.NET MVC Application (.NET Framework)
  • Install Microsoft.AspNetCore.DataProtection.SystemWeb package
  • Configure Data Protection:
    services.AddDataProtection().SetApplicationName("demo").PersistKeysToFileSystem(new DirectoryInfo( Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "..", "keys") ));
  • Add form:
    <form action="http://localhost:1055/home/index" method="post"> @Html.AntiForgeryToken() <button type="submit">Send</button> </form>
  • Add another web app (.NET 5)
  • Configure Data Protection the same way
  • Add a controller to handle requests from the first app
  • Try to validate request _antiforgery.ValidateRequestAsync(HttpContext)

Exceptions (if any)

The antiforgery token could not be decrypted.

at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.DeserializeTokens(HttpContext httpContext, AntiforgeryTokenSet antiforgeryTokenSet, AntiforgeryToken& cookieToken, AntiforgeryToken& requestToken)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateTokens(HttpContext httpContext, AntiforgeryTokenSet antiforgeryTokenSet)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.d__9.MoveNext()

InnerException:

The payload was invalid.

at Microsoft.AspNetCore.DataProtection.Cng.CbcAuthenticatedEncryptor.DecryptImpl(Byte* pbCiphertext, UInt32 cbCiphertext, Byte* pbAdditionalAuthenticatedData, UInt32 cbAdditionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.Cng.Internal.CngAuthenticatedEncryptorBase.Decrypt(ArraySegment1 ciphertext, ArraySegment1 additionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)

.NET Version

4.7.2;5.0

Anything else?

Repo: https://github.com/ptzremote/InvalidPayload

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-dataprotection Includes: DataProtection label Apr 19, 2023
@amcasey
Copy link
Member

amcasey commented May 20, 2023

@ptzremote Thanks for the report! Are you only seeing that when the two apps use different versions of .net or does it happen with two 4.5.7 or two 5.0 apps?

@ProVega
Copy link

ProVega commented Jul 22, 2023

Any luck?

I have the same issue between a .NET 4.8 MVC web app and .NET 6.0 ASP Web API App.

I got past the Key Ring error and not finding the header and cookie errors (so I believe this is really just an encryption? problem)

Both are configured to use the same shared key from File system

	public override void ConfigureServices(IServiceCollection services)
	{
		services.AddDataProtection()
			.SetApplicationName("my-app")
			.PersistKeysToFileSystem(new DirectoryInfo("C:\\keyDirectory"))
							.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration
{
				EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
				ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
			});
	}

at Microsoft.AspNetCore.DataProtection.Cng.CbcAuthenticatedEncryptor.DecryptImpl(Byte* pbCiphertext, UInt32 cbCiphertext, Byte* pbAdditionalAuthenticatedData, UInt32 cbAdditionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.Cng.Internal.CngAuthenticatedEncryptorBase.Decrypt(ArraySegment1 ciphertext, ArraySegment1 additionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)

The ASP.NET app is configured to use Microsoft.AspNetCore.DataProtection.SystemWeb

	<!--

If you want to customize the behavior of the ASP.NET Core Data Protection stack, set the
"aspnet:dataProtectionStartupType" switch below to be the fully-qualified name of a
type which subclasses Microsoft.AspNetCore.DataProtection.SystemWeb.DataProtectionStartup.
-->

@amcasey
Copy link
Member

amcasey commented Jan 23, 2024

This sounds very similar to #39958.

@amcasey
Copy link
Member

amcasey commented Jan 26, 2024

Closing as a dup, pending new information.

@amcasey amcasey closed this as completed Jan 26, 2024
@enkelmedia
Copy link

Might be related to this: umbraco/Umbraco-CMS#16107

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

4 participants