Nec*_*ras 1 c# asp.net-mvc umbraco nwebsec
我正在使用Umbraco CMS版本7的网站上工作.我正在使用NWebSec在网站上实现CSP标头.NWebSec具有内置功能,可在发生CSP违规时引发.Net事件.通常你会用这样的事情来抓住那个事件:
protected void NWebSecHttpHeaderSecurityModule_CspViolationReported(object sender, CspViolationReportEventArgs e)
{
var report = e.ViolationReport;
var serializedReport = JsonConvert.SerializeObject(report.Details);
// Do a thing with the report
}
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs文件中.但据我所知,Umbraco抢占了Global.asax.cs文件,它可以播放任何被抛出的事件.我有一个文件,其中包含一些自定义事件处理程序,如:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
Run Code Online (Sandbox Code Playgroud)
处理通常在Global.asax.cs文件中的标准应用程序启动代码,但是将NWebSec事件处理程序放在同一个文件中并不起作用.大概是因为它使用.Net事件处理程序语法而不是Umbraco替换它的任何东西.
如何访问NWebSec抛出的事件?
Global.asax类继承自UmbracoApplication不,你不能使用它.这有很多原因,包括启用在Web上下文之外"运行"Umbraco的能力 - 即在控制台应用程序中.
在查看NWebSec文档网站上的可用文档之后,我认为您不能将NWebSecHttpHeaderSecurityModule_CspViolationReported事件处理程序方法放在类中,您还需要将其连接起来.它应该看起来像这样:
public class MyGlobalEventHandler : ApplicationEventHandler {
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var nWebSecHttpHeaderSecurityModule = umbracoApplication.Modules["NWebSecHttpHeaderSecurityModule"] as HttpHeaderSecurityModule;
if (nWebSecHttpHeaderSecurityModule != null) {
nWebSecHttpHeaderSecurityModule.CspViolationReported += NWebSecHttpHeaderSecurityModule_CspViolationReported;
}
base.ApplicationStarted(umbracoApplication, applicationContext);
}
protected void NWebSecHttpHeaderSecurityModule_CspViolationReported(object sender, CspViolationReportEventArgs e)
{
var report = e.ViolationReport;
var serializedReport = JsonConvert.SerializeObject(report.Details);
// Do a thing with the report
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是支持OWIN(7.3.0)的更新版本的Umbraco,您可以使用该NWebsec.Owin库,这可能会给您带来更好的结果和更大的灵活性.
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |