From 6e299d695f0033a744d62b9a6b4d1add947d19e1 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Wed, 25 Nov 2015 09:46:36 -0800 Subject: [PATCH] Update `DefaultHtmlGenerator.GenerateAntiforgery()` to match latest `IAntiforgery.GetHtml()` - #3123 (3 of 5 or so) - react to rest of aspnet/Antiforgery@6a9b38d - remove `HtmlEncoder` from localization requirements - literal `hidden` is no longer HTML encoded (was a no-op anyhow) --- .../Internal/MvcLocalizationServices.cs | 7 ------- .../ViewFeatures/DefaultHtmlGenerator.cs | 3 +-- .../ViewFeatures/IHtmlGenerator.cs | 10 ++++++---- .../project.json | 3 ++- .../AntiforgeryTestHelper.cs | 7 +++---- ...WebSite.HtmlGeneration_Home.Order.Encoded.html | 2 +- ...ration_Home.OrderUsingHtmlHelpers.Encoded.html | 2 +- ...LocalizationServiceCollectionExtensionsTest.cs | 15 --------------- 8 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Localization/Internal/MvcLocalizationServices.cs b/src/Microsoft.AspNet.Mvc.Localization/Internal/MvcLocalizationServices.cs index 9736baccae..595cb9bb7e 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/Internal/MvcLocalizationServices.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/Internal/MvcLocalizationServices.cs @@ -2,13 +2,10 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; -using System.Text.Encodings.Web; using Microsoft.AspNet.Mvc.Razor; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Localization; -using Microsoft.Extensions.WebEncoders; namespace Microsoft.AspNet.Mvc.Localization.Internal { @@ -28,10 +25,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>))); services.TryAdd(ServiceDescriptor.Transient()); - if (!services.Any(sd => sd.ServiceType == typeof(HtmlEncoder))) - { - services.TryAdd(ServiceDescriptor.Instance(HtmlEncoder.Default)); - } services.AddLocalization(setupAction); } diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs index fba7470525..6ded798a99 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs @@ -132,8 +132,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures throw new ArgumentNullException(nameof(viewContext)); } - var tag = _antiforgery.GetHtml(viewContext.HttpContext); - return new HtmlString(tag); + return _antiforgery.GetHtml(viewContext.HttpContext); } /// diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/IHtmlGenerator.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/IHtmlGenerator.cs index eb84322553..7ac2d2f441 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/IHtmlGenerator.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/IHtmlGenerator.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using Microsoft.AspNet.Html.Abstractions; -using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.AspNet.Mvc.Rendering; @@ -33,14 +32,17 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures object htmlAttributes); /// - /// Genrate an <input type="hidden".../> element containing an antiforgery token. + /// Generate an <input type="hidden".../> element containing an antiforgery token. /// /// The instance for the current scope. - /// An instance for the <input type="hidden".../> element. + /// + /// An instance for the <input type="hidden".../> element. Intended to be used + /// inside a <form> element. + /// IHtmlContent GenerateAntiforgery(ViewContext viewContext); /// - /// Generate a <input type="checkbox".../> element. + /// Generate a <input type="checkbox".../> element. /// /// The instance for the current scope. /// The for the model. diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/project.json b/src/Microsoft.AspNet.Mvc.ViewFeatures/project.json index f772d7a512..c50b17a84c 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/project.json +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/project.json @@ -31,7 +31,8 @@ "Microsoft.Extensions.PropertyHelper.Sources": { "version": "1.0.0-*", "type": "build" - } + }, + "Microsoft.Extensions.WebEncoders": "1.0.0-*" }, "frameworks": { "net451": {}, diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs index 1e50ef4514..4b8de63b96 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs @@ -41,10 +41,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests { if (input.Attribute("name") != null && input.Attribute("type") != null && - (input.Attribute("name").Value == "__RequestVerificationToken" && - input.Attribute("type").Value == "hidden" || - input.Attribute("name").Value == "HtmlEncode[[__RequestVerificationToken]]" && - input.Attribute("type").Value == "HtmlEncode[[hidden]]")) + input.Attribute("type").Value == "hidden" && + (input.Attribute("name").Value == "__RequestVerificationToken" || + input.Attribute("name").Value == "HtmlEncode[[__RequestVerificationToken]]")) { yield return input.Attributes("value").First().Value; } diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html index 16d8ba1fbf..2d882b76e3 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html @@ -78,6 +78,6 @@ - + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html index 8169d7284b..93eda8f2fc 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html @@ -77,5 +77,5 @@ - + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs index 97d5762800..b685f2076d 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs @@ -55,11 +55,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal Assert.Equal(ServiceLifetime.Transient, service.Lifetime); }, service => - { - Assert.Equal(typeof(HtmlEncoder), service.ServiceType); - Assert.Equal(ServiceLifetime.Singleton, service.Lifetime); - }, - service => { Assert.Equal(typeof(IStringLocalizerFactory), service.ServiceType); Assert.Equal(typeof(ResourceManagerStringLocalizerFactory), service.ImplementationType); @@ -197,11 +192,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal Assert.Equal(ServiceLifetime.Transient, service.Lifetime); }, service => - { - Assert.Equal(typeof(HtmlEncoder), service.ServiceType); - Assert.Equal(ServiceLifetime.Singleton, service.Lifetime); - }, - service => { Assert.Equal(typeof(IStringLocalizerFactory), service.ServiceType); Assert.Equal(typeof(ResourceManagerStringLocalizerFactory), service.ImplementationType); @@ -276,11 +266,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal Assert.Equal(ServiceLifetime.Transient, service.Lifetime); }, service => - { - Assert.Equal(typeof(HtmlEncoder), service.ServiceType); - Assert.Equal(ServiceLifetime.Singleton, service.Lifetime); - }, - service => { Assert.Equal(typeof(IStringLocalizerFactory), service.ServiceType); Assert.Equal(typeof(ResourceManagerStringLocalizerFactory), service.ImplementationType);