diff --git a/src/Microsoft.AspNetCore.Mvc.TagHelpers/FormTagHelper.cs b/src/Microsoft.AspNetCore.Mvc.TagHelpers/FormTagHelper.cs
index b37369bbd4..d9907d99fc 100644
--- a/src/Microsoft.AspNetCore.Mvc.TagHelpers/FormTagHelper.cs
+++ b/src/Microsoft.AspNetCore.Mvc.TagHelpers/FormTagHelper.cs
@@ -242,7 +242,15 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
}
TagBuilder tagBuilder = null;
- if (Action == null && Controller == null && Route == null && _routeValues == null && Fragment == null && Area == null && Page == null)
+ if (Action == null &&
+ Controller == null &&
+ Route == null &&
+ _routeValues == null &&
+ Fragment == null &&
+ Area == null &&
+ Page == null &&
+ // Antiforgery will sometime be set globally via TagHelper Initializers, verify it was provided in the cshtml.
+ !context.AllAttributes.ContainsName(AntiforgeryAttributeName))
{
// Empty form tag such as
. Let it flow to the output as-is and only handle anti-forgery.
}
diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.html b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.html
index 24c01c7601..8529081f48 100644
--- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.html
+++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.html
@@ -1,4 +1,4 @@
-
+
Form
@@ -8,6 +8,7 @@
Form Tag Helper Test
+
diff --git a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/FormTagHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/FormTagHelperTest.cs
index aaea09b846..03fde2f5b4 100644
--- a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/FormTagHelperTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/FormTagHelperTest.cs
@@ -25,6 +25,60 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
{
public class FormTagHelperTest
{
+ [Fact]
+ public async Task ProcessAsync_AspAntiforgeryAloneGeneratesProperFormTag()
+ {
+ // Arrange
+ var expectedTagName = "form";
+ var metadataProvider = new TestModelMetadataProvider();
+ var tagHelperContext = new TagHelperContext(
+ tagName: "form",
+ allAttributes: new TagHelperAttributeList()
+ {
+ { "asp-antiforgery", true }
+ },
+ items: new Dictionary