FoxLearn.AspNet.SecurityHeaders
1.0.1
dotnet add package FoxLearn.AspNet.SecurityHeaders --version 1.0.1
NuGet\Install-Package FoxLearn.AspNet.SecurityHeaders -Version 1.0.1
<PackageReference Include="FoxLearn.AspNet.SecurityHeaders" Version="1.0.1" />
<PackageVersion Include="FoxLearn.AspNet.SecurityHeaders" Version="1.0.1" />
<PackageReference Include="FoxLearn.AspNet.SecurityHeaders" />
paket add FoxLearn.AspNet.SecurityHeaders --version 1.0.1
#r "nuget: FoxLearn.AspNet.SecurityHeaders, 1.0.1"
#:package FoxLearn.AspNet.SecurityHeaders@1.0.1
#addin nuget:?package=FoxLearn.AspNet.SecurityHeaders&version=1.0.1
#tool nuget:?package=FoxLearn.AspNet.SecurityHeaders&version=1.0.1
๐ท FoxLearn.AspNet.SecurityHeaders
FoxLearn.AspNet.SecurityHeaders is a lightweight library for adding common security headers to ASP.NET applications. It simplifies the process of applying essential HTTP security headers like Content-Security-Policy
, Strict-Transport-Security
, X-Content-Type-Options
, and others helping protect your applications from common web vulnerabilities.
โ Features
- ๐ Easy integration with ASP.NET MVC and WebForms
- โ๏ธ Predefined defaults for recommended security headers
- ๐ ๏ธ Fully customizable header values
- ๐ก Supports .NET Framework 4.5 and later
๐ฅ Installation
Install via the .NET CLI:
dotnet add package FoxLearn.AspNet.SecurityHeaders
Or via the NuGet UI in Visual Studio by searching for FoxLearn.AspNet.SecurityHeaders, then click Install.
๐งช Usage
To enable and configure security headers in your MVC application: Update FilterConfig.cs
:
using System.Web.Mvc;
using FoxLearn.AspNet.SecurityHeaders;
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
// Apply default headers
SecurityHeadersConfig.RegisterGlobalSecurityHeaders(filters);
}
}
To fully control the headers returned, create a HeaderPolicyCollection and define your own set of headers including any custom headers you may need:
using System.Web.Mvc;
using FoxLearn.AspNet.SecurityHeaders;
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
var policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
.AddContentTypeOptionsNoSniff()
.AddCustomHeader("X-Default-Header", "DefaultValue")
.AddPolicy("CustomPolicy", p =>
{
p.AddCustomHeader("X-Custom", "MyValue"); // Apply custom headers
});
SecurityHeadersConfig.RegisterGlobalSecurityHeaders(filters, policy);
}
}
๐ Apply a Policy to a Controller Action
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[SecurityHeadersPolicy("CustomPolicy")]
public IActionResult Index()
{
return View();
}
}
To enable headers in a WebForms project, update your Global.asax.cs
:
using System.Web;
using FoxLearn.AspNet.SecurityHeaders;
public class Global : HttpApplication
{
public override void Init()
{
SecurityHeadersConfig.Register(this);
base.Init();
}
}
You can also create a HeaderPolicyCollection and define your own set of headers.
using System.Web;
using FoxLearn.AspNet.SecurityHeaders;
public class Global : HttpApplication
{
public override void Init()
{
var policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
.AddContentTypeOptionsNoSniff()
SecurityHeadersConfig.Register(this, policy);
base.Init();
}
}
๐ License
This project is licensed under the MIT License. Free for personal and commercial use.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.5
- Microsoft.AspNet.Mvc (>= 5.2.9)
- Microsoft.Web.Infrastructure (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.