This commit is contained in:
2022-01-03 10:46:25 +01:00
parent a20ff88c51
commit d3a0bd4ad1
4 changed files with 31 additions and 11 deletions

View File

@@ -41,19 +41,39 @@ namespace VAR.WebFormsCore.Code
context.Response.Body.WriteAsync(byteObject);
}
public static void SafeSet(this IHeaderDictionary header, string key, string value)
{
if (header.ContainsKey(key))
{
header[key] = value;
}
else
{
header.Add(key, value);
}
}
public static void SafeDel(this IHeaderDictionary header, string key)
{
if (header.ContainsKey(key))
{
header.Remove(key);
}
}
public static void PrepareCacheableResponse(this HttpResponse response)
{
const int secondsInDay = 86400;
response.Headers.Add("Cache-Control", string.Format("public, max-age={0}", secondsInDay));
response.Headers.SafeSet("Cache-Control", string.Format("public, max-age={0}", secondsInDay));
string ExpireDate = DateTime.UtcNow.AddSeconds(secondsInDay).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
response.Headers.Add("Expires", ExpireDate + " GMT");
response.Headers.SafeSet("Expires", ExpireDate + " GMT");
}
public static void PrepareUncacheableResponse(this HttpResponse response)
{
response.Headers.Add("Cache-Control", "max-age=0, no-cache, no-store");
response.Headers.SafeSet("Cache-Control", "max-age=0, no-cache, no-store");
string ExpireDate = DateTime.UtcNow.AddSeconds(-1500).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
response.Headers.Add("Expires", ExpireDate + " GMT");
response.Headers.SafeSet("Expires", ExpireDate + " GMT");
}
#endregion HttpContext

View File

@@ -24,11 +24,11 @@ namespace VAR.WebFormsCore.Code
public async Task Invoke(HttpContext httpContext)
{
httpContext.Response.Headers.Remove("Server");
httpContext.Response.Headers.Remove("X-Powered-By");
httpContext.Response.Headers.Add("X-Content-Type-Options", "nosniff");
httpContext.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
httpContext.Response.Headers.Add("X-XSS-Protection", "1; mode=block");
httpContext.Response.Headers.SafeDel("Server");
httpContext.Response.Headers.SafeDel("X-Powered-By");
httpContext.Response.Headers.SafeSet("X-Content-Type-Options", "nosniff");
httpContext.Response.Headers.SafeSet("X-Frame-Options", "SAMEORIGIN");
httpContext.Response.Headers.SafeSet("X-XSS-Protection", "1; mode=block");
try
{

View File

@@ -59,7 +59,7 @@ namespace VAR.WebFormsCore.Pages
Render(stringWriter);
if (context.Response.HasStarted) { return; }
context.Response.Headers.Add("Content-Type", "text/html");
context.Response.Headers.SafeSet("Content-Type", "text/html");
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
await context.Response.Body.WriteAsync(byteObject);
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>