Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Upgrade to ASP.NET CORE 6.0 #178 - Further changes with Invalid non-ASCII control character in header
KrzysztofPajak1025f8e53acb
diff --git a/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs b/src/Core/Grand.Infrastructure/Configuration/AppConfig.csindex b19234823..5a2d25d8d 100644--- a/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs+++ b/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs@@ -154,7 +154,6 @@ namespace Grand.Infrastructure.Configuration/// </summary>public bool DisplayMiniProfilerInPublicStore { get; set; }- public bool AllowNonAsciiCharInHeaders { get; set; }/// <summary>/// A value indicating whether SEO friendly URLs with multiple languages are enabled/// </summary>diff --git a/src/Core/Grand.Infrastructure/Configuration/HostingConfig.cs b/src/Core/Grand.Infrastructure/Configuration/HostingConfig.csindex 2441247a2..64b000f70 100644--- a/src/Core/Grand.Infrastructure/Configuration/HostingConfig.cs+++ b/src/Core/Grand.Infrastructure/Configuration/HostingConfig.cs@@ -11,5 +11,11 @@/// Gets or sets a value for allowedHosts, is used for host filtering to bind your app to specific hostnames/// </summary>public string AllowedHosts { get; set; }++ /// <summary>+ /// Gets or sets a value custom response header encoding may be needed in some cases+ /// </summary>+ public bool AllowNonAsciiCharInHeaders { get; set; }+}}diff --git a/src/Web/Grand.Web.Common/Filters/LanguageAttribute.cs b/src/Web/Grand.Web.Common/Filters/LanguageAttribute.csindex 0000355ec..284cce063 100644--- a/src/Web/Grand.Web.Common/Filters/LanguageAttribute.cs+++ b/src/Web/Grand.Web.Common/Filters/LanguageAttribute.cs@@ -103,7 +103,7 @@ namespace Grand.Web.Common.Filtersreturn;}- pageUrl = AddLanguageSeo(pageUrl, context.HttpContext.Request.PathBase, _workContext.WorkingLanguage);+ pageUrl = AddLanguageSeo(pageUrl, _workContext.WorkingLanguage);context.Result = new RedirectResult(pageUrl, false);}@@ -123,7 +123,7 @@ namespace Grand.Web.Common.Filtersreturn language != null ? language.Published : false;}- private string AddLanguageSeo(string url, PathString pathBase, Language language)+ private string AddLanguageSeo(string url, Language language){if (language == null)throw new ArgumentNullException(nameof(language));@@ -131,13 +131,11 @@ namespace Grand.Web.Common.Filters//remove application path from raw URLif (!string.IsNullOrEmpty(url)){- var _ = new PathString(url).StartsWithSegments(pathBase, out PathString result);- url = WebUtility.UrlDecode(result);+ url = Flurl.Url.EncodeIllegalCharacters(url);}//add language code- url = $"/{language.UniqueSeoCode}{url}";- url = pathBase + url;+ url = $"/{language.UniqueSeoCode}/{url.TrimStart('/')}";return url;}diff --git a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.csindex 9ff0b27c5..cb49b967a 100644--- a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs+++ b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs@@ -11,13 +11,10 @@ using Grand.Infrastructure.Plugins;using Grand.Infrastructure.TypeSearchers;using Grand.SharedKernel.Extensions;using Grand.Web.Common.Extensions;-using Grand.Web.Common.Routing;using Grand.Web.Common.Themes;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.DataProtection;using Microsoft.AspNetCore.Http;-using Microsoft.AspNetCore.Mvc;-using Microsoft.AspNetCore.Mvc.Infrastructure;using Microsoft.AspNetCore.Mvc.Razor;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;@@ -253,16 +250,6 @@ namespace Grand.Web.Common.Infrastructure});}- /// <summary>- /// Register custom RedirectResultExecutor- /// </summary>- /// <param name="services">Collection of service descriptors</param>- public static void AddGrandRedirectResultExecutor(this IServiceCollection services)- {- //we use custom redirect executor as a workaround to allow using non-ASCII characters in redirect URLs- services.AddSingleton<IActionResultExecutor<RedirectResult>, GrandRedirectResultExecutor>();- }-public static void AddSettings(this IServiceCollection services){var typeSearcher = new AppTypeSearcher();diff --git a/src/Web/Grand.Web.Common/Routing/GrandRedirectResultExecutor.cs b/src/Web/Grand.Web.Common/Routing/GrandRedirectResultExecutor.csdeleted file mode 100644index fb02c1e4d..000000000--- a/src/Web/Grand.Web.Common/Routing/GrandRedirectResultExecutor.cs+++ /dev/null@@ -1,54 +0,0 @@-using Grand.Infrastructure.Configuration;-using Microsoft.AspNetCore.Mvc;-using Microsoft.AspNetCore.Mvc.Infrastructure;-using Microsoft.AspNetCore.Mvc.Routing;-using Microsoft.Extensions.Logging;-using System;-using System.Threading.Tasks;--namespace Grand.Web.Common.Routing-{- /// <summary>- /// Represents custom overridden redirect result executor- /// </summary>- public class GrandRedirectResultExecutor : RedirectResultExecutor- {- #region Fields-- private readonly AppConfig _config;-- #endregion-- #region Ctor-- public GrandRedirectResultExecutor(ILoggerFactory loggerFactory,- IUrlHelperFactory urlHelperFactory,- AppConfig config) : base(loggerFactory, urlHelperFactory)- {- _config = config;- }-- #endregion-- #region Methods-- /// <summary>- /// Execute passed redirect result- /// </summary>- /// <param name="context">Action context</param>- /// <param name="result">Redirect result</param>- public override Task ExecuteAsync(ActionContext context, RedirectResult result)- {- if (result == null)- throw new ArgumentNullException(nameof(result));-- if (_config.AllowNonAsciiCharInHeaders)- {- result.Url = Flurl.Url.EncodeIllegalCharacters(result.Url);- }- return base.ExecuteAsync(context, result);- }-- #endregion- }-}diff --git a/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs b/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.csindex f064a1db0..d41baf5e7 100644--- a/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs+++ b/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs@@ -33,10 +33,7 @@ namespace Grand.Web.Common.Startup//add pwaservices.AddPWA(configuration);-- //add custom redirect result executor- services.AddGrandRedirectResultExecutor();-+}/// <summary>diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.jsonindex d53fa13bc..92e8cf06f 100644--- a/src/Web/Grand.Web/App_Data/appsettings.json+++ b/src/Web/Grand.Web/App_Data/appsettings.json@@ -4,7 +4,11 @@"UseForwardedHeaders": false,//AllowedHosts, is used for host filtering to bind your app to specific hostnames- "AllowedHosts": ""+ "AllowedHosts": "",++ //Note: While use of custom response header encoding may be needed in some cases,+ //we discourage the use of non-ASCII encodings to avoid compatibility issues with other HTTP clients.+ "AllowNonAsciiCharInHeaders": false},"Application": {@@ -116,8 +120,6 @@//Gets or sets a value indicating whether mini profiler should be displayed in public store (used for debugging)"DisplayMiniProfilerInPublicStore": false,- "AllowNonAsciiCharInHeaders": true,-//A value indicating whether SEO friendly URLs with multiple languages are enabled"SeoFriendlyUrlsForLanguagesEnabled": false,"SeoFriendlyUrlsDefaultCode": "en",diff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.csindex 10919e27d..d2cc27b8d 100644--- a/src/Web/Grand.Web/Controllers/CommonController.cs+++ b/src/Web/Grand.Web/Controllers/CommonController.cs@@ -97,23 +97,19 @@ namespace Grand.Web.Controllersreturn language != null ? language.Published : false;}- private string AddLanguageSeo(string url, PathString pathBase, Language language)++ private string AddLanguageSeo(string url, Language language){if (language == null)throw new ArgumentNullException(nameof(language));- //remove application path from raw URLif (!string.IsNullOrEmpty(url)){- var _ = new PathString(url).StartsWithSegments(pathBase, out PathString result);- url = WebUtility.UrlDecode(result);+ url = Flurl.Url.EncodeIllegalCharacters(url);}//add language code- url = $"/{language.UniqueSeoCode}{url}";- url = pathBase + url;-- return url;+ return $"/{language.UniqueSeoCode}/{url.TrimStart('/')}";}#endregion@@ -163,10 +159,10 @@ namespace Grand.Web.Controllers//language part in URLif (config.SeoFriendlyUrlsForLanguagesEnabled){- if (await IsLocalized(returnUrl, this.Request.PathBase))- returnUrl = RemoveLanguageSeoCode(returnUrl, this.Request.PathBase);+ if (await IsLocalized(returnUrl, Request.PathBase))+ returnUrl = RemoveLanguageSeoCode(returnUrl, Request.PathBase);- returnUrl = AddLanguageSeo(returnUrl, this.Request.PathBase, language);+ returnUrl = AddLanguageSeo(returnUrl, language);}await _workContext.SetWorkingLanguage(language);@@ -177,7 +173,7 @@ namespace Grand.Web.Controllersreturn Redirect(returnUrl);}- //helper method to redirect users.+ //helper method to redirect (use in SlugRouteTransformer).public virtual IActionResult InternalRedirect(string url, bool permanentRedirect){//ensure it's invoked from our GenericPathRoute class@@ -201,10 +197,12 @@ namespace Grand.Web.Controllersurl = Url.RouteUrl("HomePage");permanentRedirect = false;}- url = Uri.UnescapeDataString(WebUtility.UrlDecode(url));++ url = Flurl.Url.EncodeIllegalCharacters(url);if (permanentRedirect)return RedirectPermanent(url);+return Redirect(url);}diff --git a/src/Web/Grand.Web/Program.cs b/src/Web/Grand.Web/Program.csindex d2f033a79..ee08fe016 100644--- a/src/Web/Grand.Web/Program.cs+++ b/src/Web/Grand.Web/Program.cs@@ -1,8 +1,11 @@-using Grand.Web.Common.Startup;+using Grand.Infrastructure.Configuration;+using Grand.Web.Common.Startup;using Microsoft.AspNetCore.Builder;+using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.Hosting;using Serilog;+using System.Text;var builder = WebApplication.CreateBuilder(args);@@ -12,6 +15,7 @@ builder.Host.UseDefaultServiceProvider((context, options) =>options.ValidateOnBuild = false;});+//use serilogbuilder.Host.UseSerilog();@@ -30,6 +34,7 @@ builder.Host.ConfigureAppConfiguration((hostingContext, config) =>if (!string.IsNullOrEmpty(appsettings) && !string.IsNullOrEmpty(param))config.AddJsonFile($"App_Data/{param}/appsettings.json", optional: false, reloadOnChange: true);}+});//create logger@@ -38,6 +43,17 @@ Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configurat//add servicesGrand.Infrastructure.StartupBase.ConfigureServices(builder.Services, builder.Configuration);+//Allow non ASCII chars in headers+var config = new HostingConfig();+builder.Configuration.GetSection("Application").Bind(config);+if(config.AllowNonAsciiCharInHeaders)+{+ builder.WebHost.ConfigureKestrel(options =>+ {+ options.ResponseHeaderEncodingSelector = (_) => Encoding.UTF8;+ });+}+//register taskbuilder.Services.RegisterTasks();