blazor - How to make an handler for invalid forgery token? - Stack Overflow

Is there a way to handle invalid fery token error in Blazor WebApp .NET 8 ?A valid antifery token was n

Is there a way to handle invalid fery token error in Blazor WebApp .NET 8 ?

A valid antifery token was not provided with the request. Add an antifery token, or disable antifery validation for this endpoint

Like a redirection or something else

UPDATE

I tried this in my Program.cs:

app.Use(async (context, next) =>
{
    await next();

    if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
    {
        context.Response.Redirect("./404");
    }

    if (context.Response.StatusCode == 400 && !context.Response.HasStarted)
    {
        context.Response.Redirect("./400");
    }
});

It should be redirect to /400 page

Is there a way to handle invalid fery token error in Blazor WebApp .NET 8 ?

A valid antifery token was not provided with the request. Add an antifery token, or disable antifery validation for this endpoint

Like a redirection or something else

UPDATE

I tried this in my Program.cs:

app.Use(async (context, next) =>
{
    await next();

    if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
    {
        context.Response.Redirect("./404");
    }

    if (context.Response.StatusCode == 400 && !context.Response.HasStarted)
    {
        context.Response.Redirect("./400");
    }
});

It should be redirect to /400 page

Share Improve this question edited Jan 31 at 22:11 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 31 at 9:47 impesigiuseppeimpesigiuseppe 133 bronze badges 3
  • Hello, what have you tried on your side ? This is quite standard mechanism of request handling so we can assume Microsoft already gives a lot of advices and examples to implement it. – AFract Commented Jan 31 at 9:50
  • Hello @AFract, thanks for the attention. I updated the question with the solution i tried to implement. – impesigiuseppe Commented Jan 31 at 15:03
  • Your update have nothing to do with antifery validation – AFract Commented Jan 31 at 15:41
Add a comment  | 

1 Answer 1

Reset to default 1

The antifery validation error is not an exception that can be caught in middleware directly because ASP.NET Core handles it before the request reaches middleware. A workaround is you could make a middleware to do antiforery validation earlier and redirect.

    public class AntiferyValidationMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly IAntifery _antifery;
        private readonly ILogger<AntiferyValidationMiddleware> _logger;

        public AntiferyValidationMiddleware(RequestDelegate next, IAntifery antifery, ILogger<AntiferyValidationMiddleware> logger)
        {
            _next = next;
            _antifery = antifery;
            _logger = logger;
        }

        public async Task Invoke(HttpContext context)
        {
            // Apply antifery validation only to unsafe methods (POST, PUT, DELETE)
            if (HttpMethods.IsPost(context.Request.Method) ||
                HttpMethods.IsPut(context.Request.Method) ||
                HttpMethods.IsDelete(context.Request.Method))
            {
                try
                {
                    await _antifery.ValidateRequestAsync(context);
                }
                catch (AntiferyValidationException ex)
                {
                    _logger.LogWarning("Antifery token validation failed: {Message}", ex.Message);
                    context.Response.Redirect("./404");
                    return;
                }
            }

            await _next(context);
        }
    }

Add the middleware

var app = builder.Build();
app.UseMiddleware<AntiferyValidationMiddleware>();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270995a4619743.html

相关推荐

  • blazor - How to make an handler for invalid forgery token? - Stack Overflow

    Is there a way to handle invalid fery token error in Blazor WebApp .NET 8 ?A valid antifery token was n

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信