Skip to content

Commit e002e7e

Browse files
Set ReasonPhrase to code InvalidCSRFToken on failed validation.
1 parent c70f1fe commit e002e7e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,11 @@ bool IsAspx(HttpContext context, string basePath)
503503
}
504504
public class CustomExceptionHandlerMiddleware
505505
{
506+
const string InvalidCSRFToken = "InvalidCSRFToken";
506507
static readonly ILog log = log4net.LogManager.GetLogger(typeof(CustomExceptionHandlerMiddleware));
507508
public async Task Invoke(HttpContext httpContext)
508509
{
510+
string httpReasonPhrase=string.Empty;
509511
Exception ex = httpContext.Features.Get<IExceptionHandlerFeature>()?.Error;
510512
HttpStatusCode httpStatusCode = (HttpStatusCode)httpContext.Response.StatusCode;
511513
if (ex!=null)
@@ -519,6 +521,7 @@ public async Task Invoke(HttpContext httpContext)
519521
{
520522
//"The required antiforgery header value "X-GXCSRF-TOKEN" is not present.
521523
httpStatusCode = HttpStatusCode.BadRequest;
524+
httpReasonPhrase = InvalidCSRFToken;
522525
GXLogging.Error(log, $"Validation of antiforgery failed", ex);
523526
}
524527
else
@@ -538,6 +541,12 @@ public async Task Invoke(HttpContext httpContext)
538541
{
539542
httpContext.Response.StatusCode = (int)httpStatusCode;
540543
}
544+
if (!string.IsNullOrEmpty(httpReasonPhrase))
545+
{
546+
IHttpResponseFeature responseReason = httpContext.Response.HttpContext.Features.Get<IHttpResponseFeature>();
547+
if (responseReason!=null)
548+
responseReason.ReasonPhrase = httpReasonPhrase;
549+
}
541550
}
542551
await Task.CompletedTask;
543552
}

0 commit comments

Comments
 (0)