AntiForgeryToken Expiration Blank Page

后端 未结 3 1537
情书的邮戳
情书的邮戳 2021-02-20 03:17

I\'m using IdentityServer4 with ASP.NET Core 2.2. On the Post Login method I have applied the ValidateAntiForgeryToken. Generally after 20 minutes to 2 hours of sitting on the l

3条回答
  •  悲哀的现实
    2021-02-20 03:50

    Slight modification to d_f code https://stackoverflow.com/a/56383473/841898 Instead of page redirect we just add error to ModelState. Then we display in model state summary.

    public class CustomAntiForgeryTokenAttribute : TypeFilterAttribute
    {
        public CustomAntiForgeryTokenAttribute() : base(typeof(AnotherAntiforgeryFilter))
        {
        }
    }
    
    
    public class AnotherAntiforgeryFilter : ValidateAntiforgeryTokenAuthorizationFilter,
        IAsyncAuthorizationFilter
    {
        public AnotherAntiforgeryFilter(IAntiforgery a, ILoggerFactory l) : base(a, l)
        {
        }
    
        async Task IAsyncAuthorizationFilter.OnAuthorizationAsync(
            AuthorizationFilterContext ctx)
        {
            await base.OnAuthorizationAsync(ctx);
    
            if (ctx.Result is IAntiforgeryValidationFailedResult)
            {
                ctx.ModelState.AddModelError("Token", "Validation Token Expired. Please try again");
                ctx.Result = null;
    
            }
        }
    }
    

提交回复
热议问题