我正在开发一个使用Umbraco版本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
}在Global.asax.cs文件中。但据我所知,Umbraco抢占了Global.asax.cs文件,并且它会吃掉任何抛出的事件。我有一个带有一些自定义事件处理程序的文件,如:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)要处理通常位于Global.asax.cs文件中的标准应用程序启动代码,但是将NWebSec事件处理程序放在同一个文件中并不有效。这大概是因为它使用的是.Net事件处理程序语法,而不是用任何Umbraco替换它。
如何访问NWebSec引发的事件?
发布于 2015-10-23 05:16:36
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
}
}如果使用支持OWIN (7.3.0)的更新版本的Umbraco,则可以使用NWebsec.Owin库,这可能会给您带来更好的结果和更大的灵活性。
https://stackoverflow.com/questions/33287924
复制相似问题