Skip to content

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.

Add new security settings EnableRuntimeCompilation (#342)

Krzysztof Pajak4d7e70760944
diff --git a/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs b/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
index 0458ff729..560a963dd 100644
--- a/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
+++ b/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
@@ -54,5 +54,10 @@
public int HttpsRedirectionRedirect { get; set; }
public int? HttpsRedirectionHttpsPort { get; set; }
+
+ /// <summary>
+ /// When enabled, allowing Razor files to be updated if they're edited.
+ /// </summary>
+ public bool EnableRuntimeCompilation { get; set; }
}
}
diff --git a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
index 706033538..379eea577 100644
--- a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
+++ b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
@@ -14,6 +14,7 @@
<PackageReference Include="WebMarkupMin.AspNetCore6" Version="2.12.0" />
<PackageReference Include="WebMarkupMin.Core" Version="2.12.0" />
<PackageReference Include="WebMarkupMin.NUglify" Version="2.12.0" />
+ <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.10" />
</ItemGroup>
<ItemGroup>
diff --git a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
index fcd147c41..a869c87d2 100644
--- a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
+++ b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
@@ -210,8 +210,7 @@ namespace Grand.Web.Common.Infrastructure
var securityConfig = new SecurityConfig();
configuration.GetSection("Security").Bind(securityConfig);
- var appConfig = new AppConfig();
- configuration.GetSection("Application").Bind(appConfig);
+ if(securityConfig.EnableRuntimeCompilation) mvcBuilder.AddRazorRuntimeCompilation();
if (securityConfig.UseHsts)
{
@@ -230,6 +229,10 @@ namespace Grand.Web.Common.Infrastructure
options.HttpsPort = securityConfig.HttpsRedirectionHttpsPort;
});
}
+
+ var appConfig = new AppConfig();
+ configuration.GetSection("Application").Bind(appConfig);
+
//use session-based temp data provider
if (appConfig.UseSessionStateTempDataProvider)
{
diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.json
index 10e12e150..1e8b78adb 100644
--- a/src/Web/Grand.Web/App_Data/appsettings.json
+++ b/src/Web/Grand.Web/App_Data/appsettings.json
@@ -67,6 +67,9 @@
//HTTP Strict Transport Security Protocol
"UseHsts": false,
+ //When enabled, allow Razor files to be updated if they're edited.
+ "EnableRuntimeCompilation": false,
+
//We recommend all ASP.NET Core web apps call HTTPS Redirection Middleware to redirect all HTTP requests to HTTPS
"UseHttpsRedirection": false,
"HttpsRedirectionRedirect": 308,
diff --git a/src/Web/Grand.Web/Grand.Web.csproj b/src/Web/Grand.Web/Grand.Web.csproj
index a0d83d3a7..8288b74a2 100644
--- a/src/Web/Grand.Web/Grand.Web.csproj
+++ b/src/Web/Grand.Web/Grand.Web.csproj
@@ -30,10 +30,7 @@
<ProjectReference Include="..\..\Business\Grand.Business.Storage\Grand.Business.Storage.csproj" />
<ProjectReference Include="..\..\Business\Grand.Business.System\Grand.Business.System.csproj" />
</ItemGroup>
-
- <ItemGroup>
- <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.10" />
- </ItemGroup>
+
<ItemGroup>
<Content Include="App_Data\**" CopyToPublishDirectory="PreserveNewest" Exclude="App_Data\appsettings.json;App_Data\DataProtectionKeys\*.xml;" />
<Content Include="Themes\**" CopyToPublishDirectory="PreserveNewest" Exclude="Themes\**\*.cfg;Themes\**\*.cshtml" />
diff --git a/src/Web/Grand.Web/Program.cs b/src/Web/Grand.Web/Program.cs
index fe913794b..872cf4106 100644
--- a/src/Web/Grand.Web/Program.cs
+++ b/src/Web/Grand.Web/Program.cs
@@ -68,11 +68,6 @@ if (config.MaxRequestBodySize.HasValue)
//register task
builder.Services.RegisterTasks();
-if (builder.Environment.IsDevelopment())
-{
- builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
-}
-
//build app
var app = builder.Build();