-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Antiforgery Token could not be decrypted #7364
Comments
@natemcmaster, did we regress this? According to the referenced issue this has been fixed a while ago. |
There are lots of reasons a token could not be decrypted. I would need to see more details. cc @blowdart |
@vankampenp , can you please share with us a small repro project, so we can investigate it? Thanks! |
It is a bit hard to make a repro project and try it on the production server. I do have more info. Please see the log of the event below. Please let me know if this helps. Otherwize I can try to find the smallest repro project. I first had [AutoValidateAntiforgeryToken] on the controller and removed that to see if the problem was related to that, as it seems that it is trying to get an AntiforgeryToken on an HttpGet method.
In the log, I see the arrival at method SwitchBoard(), with ModelState valid.
|
Is this a SPA app by any chance? |
It is multi-page, with the html rendered server side as razor pages. But there are some SPA like aspect, such as loading the pages through AJAX, and having one entry point to the app (with no user links). |
@vankampenp can you provide more information about how you have configured data protection? This error normally appears when the keys used by antiforgery change between the time the antiforgery token was created and when it was decrypted. Some indications this may be happening:
Are any of these true of your app? |
|
@vankampenp it's hard to investigate this further because we still don't have an way to reproduce the error. From what you've described, it could be:
@mkArtakMSFT regardless, there is still an actionable item here. We could add add more details to the log mesage about what happened. The place where the error comes from may have an inner exception. When present, we should consider logging the message from that inner exception to give us more clues about what is going wrong. |
@mkArtakMSFT un-assigning myself from further investigation. Without a repro, I cannot make progress on this. |
Thanks @natemcmaster. |
Closing this issue as it seems there is no repro and we have no way to investigate the reported behavior. Feel free to reopen if, when you'll get more info which can help with investigation. |
I get this type of error frequently...
I am using [AutoValidateAntiforgeryToken] for all controllers and the client side app is an Angular SPA. services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
...
app.UseAntiforgeryToken(); public class AntiforgeryTokenMiddleware
{
private readonly RequestDelegate _next;
private readonly IAntiforgery _antiforgery;
public AntiforgeryTokenMiddleware(RequestDelegate next, IAntiforgery antiforgery)
{
_next = next;
_antiforgery = antiforgery;
}
public Task Invoke(HttpContext context)
{
var path = context.Request.Path.Value?.ToLower() ?? string.Empty;
if (path.StartsWith("/account") || path.StartsWith("/api"))
{
var tokens = _antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
new CookieOptions() { HttpOnly = false });
}
return _next(context);
}
}
public static class AntiforgeryTokenMiddlewareExtensions
{
public static IApplicationBuilder UseAntiforgeryToken(this IApplicationBuilder builder)
{
return builder.UseMiddleware<AntiforgeryTokenMiddleware>();
}
} I am still watching the pattern when the error occurs. I strongly suspect that the end point that triggers the exception is in |
As a workaround, I am still using the solution described here aspnet/DataProtection#233. Not sure this is still needed in 2.1, but I am hesitant to remove it. |
This seems related to this issue: aspnet/DataProtection#233
On my production server, even under small load, I am getting decryption errors in the log:
Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery: An exception was thrown while deserializing the token. The antiforgery token could not be decrypted.
I am using Microsoft.AspNetCore.All 2.0.5
With the workaround described in the mentioned issue, the problem goes away
The text was updated successfully, but these errors were encountered: