as is
This commit is contained in:
@@ -24,26 +24,32 @@ public class GlobalExceptionHandlerMiddleware
|
||||
try
|
||||
{
|
||||
await _next(httpContext);
|
||||
}
|
||||
catch (BadHttpRequestException ex) when (ex.InnerException is AntiforgeryValidationException)
|
||||
{
|
||||
var problemDetails = new ProblemDetails
|
||||
httpContext.Response.OnStarting(async () =>
|
||||
{
|
||||
Detail = ex.InnerException.Message,
|
||||
Instance = httpContext.Request.Path,
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "CSRF 错误",
|
||||
};
|
||||
var antiforgeryFeature = httpContext.Features.Get<IAntiforgeryValidationFeature>();
|
||||
if (null != antiforgeryFeature && !antiforgeryFeature.IsValid)
|
||||
{
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Detail = antiforgeryFeature.Error?.Message,
|
||||
Instance = httpContext.Request.Path,
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "CSRF 错误",
|
||||
};
|
||||
|
||||
problemDetails.AddErrorCode(ProblemDetailsCodes.CsrfValidationFailed);
|
||||
problemDetails.AddErrorCode(ProblemDetailsCodes.CsrfValidationFailed);
|
||||
|
||||
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
httpContext.Response.ContentType = "application/problem+json";
|
||||
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
httpContext.Response.ContentType = "application/problem+json";
|
||||
|
||||
await _problemDetailsService.WriteAsync(new ProblemDetailsContext
|
||||
{
|
||||
HttpContext = httpContext,
|
||||
ProblemDetails = problemDetails
|
||||
await _problemDetailsService.WriteAsync(new ProblemDetailsContext
|
||||
{
|
||||
HttpContext = httpContext,
|
||||
ProblemDetails = problemDetails
|
||||
});
|
||||
}
|
||||
|
||||
await Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
catch (BadHttpRequestException ex)
|
||||
|
||||
Reference in New Issue
Block a user