在 Application_BeginRequest 期间检查静态文件?
我有一个 Global.asx 文件,需要执行自定义身份验证、审核和分析工作。这是必需的,因为它支持基于 SAML 的 SSO 系统,并且需要覆盖正常的 .Net 身份验证(不支持 SAML 或混合身份验证),
我不想为静态文件(例如 )触发它。 js
、.css
、.png
等
在 Cassini/WebDev 和 IIS7 中确实如此。
我想要的是一些简单的检查,例如 this.Request.IsStaticFile
(不幸的是,它不存在)来识别静态文件。
我意识到这写起来相当简单,但感觉就像一些必须已经存在的东西——IIS 已经为静态文件等应用了缓存策略。
我需要一种代码解决方案,而不是 IIS 配置更改解决方案。
更新
这是我当前的解决方法:
/// <summary>Hold all the extensions we treat as static</summary>
static HashSet<string> allowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".js", ".css", ".png", ...
};
/// <summary>Is this a request for a static file?</summary>
/// <param name="request">The HTTP request instance to extend.</param>
/// <returns>True if the request is for a static file on disk, false otherwise.</returns>
public static bool IsStaticFile(this HttpRequest request)
{
string fileOnDisk = request.PhysicalPath;
if (string.IsNullOrEmpty(fileOnDisk))
{
return false;
}
string extension = Path.GetExtension(fileOnDisk);
return allowedExtensions.Contains(extension);
}
这有效并且足够快,但感觉非常笨重。特别是,如果我们添加未想到的新静态文件,依赖扩展将很容易出错。
有没有更好的方法而不改变IIS配置?
I have a Global.asx file that needs to do custom authentication, auditing and profiling stuff. This is needed because it supports a SAML based SSO system and needs to override the normal .Net authentication (which doesn't support either SAML or mixed authentication)
I don't want to fire it for static files, such as .js
, .css
, .png
, etc
In Cassini/WebDev and IIS7 it does.
What I want to have is some simple check, like a this.Request.IsStaticFile
(which doesn't exist, unfortunately) to identify the static files.
I realise that this would be fairly simple to write, but it feels like something that must already exist - IIS has already applied caching policy stuff for the static files and so on.
I need a code solution, rather than an IIS config change one.
Update
This is my current workaround:
/// <summary>Hold all the extensions we treat as static</summary>
static HashSet<string> allowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".js", ".css", ".png", ...
};
/// <summary>Is this a request for a static file?</summary>
/// <param name="request">The HTTP request instance to extend.</param>
/// <returns>True if the request is for a static file on disk, false otherwise.</returns>
public static bool IsStaticFile(this HttpRequest request)
{
string fileOnDisk = request.PhysicalPath;
if (string.IsNullOrEmpty(fileOnDisk))
{
return false;
}
string extension = Path.GetExtension(fileOnDisk);
return allowedExtensions.Contains(extension);
}
This works and is quick enough, but feels horribly clunky. In particular relying on extensions is going to be error prone if we add new static files not thought of.
Is there a better way without changing the IIS config?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您也许可以检查哪个处理程序正在处理该请求。
在 IIS6 中,只有 .net 文件(例如 aspx)被映射到执行某些操作的处理程序。
在具有集成管道的 IIS7 中,所有内容都通过 .net 路由,这通常是一件好事。不过,不同的处理程序仍然处理不同的文件类型。我特别相信 staticfilehandler 是您需要检查的一个。
httpcontext.handler
属性应该可以让您弄清楚。您可以创建一个扩展方法来添加 IsStatic 方法...
Simon
You might be able to check which handler is dealing with the request.
In IIS6 only .net files, eg aspx are mapped to a handler that does stuff.
In IIS7 with the integrated pipeline, everything routes through .net, which is normally a good thing. Different handlers still deal with different file types though. In particular I believe the staticfilehandler is the one you need to check for. The
httpcontext.handler
property should allow you to figure it out.You could create an extension method to add that IsStatic method...
Simon
有几个选项:
authorization
元素 并拒绝那些不需要任何身份验证并包含静态文件的路径There are a few options:
authorization
element and deny none for those paths that you do not need any authentication and contains your static files毫无疑问,您需要创建一个自定义扩展方法,因为 ASP.NET 路由引擎使用此代码来确定文件是否存在,
您将无法使用 context.handler 来确定 Application_BeginRequest 中的请求是否是静态的,因为路由模块可能会更改处理程序,该模块始终在 Application_BeginRequest 之后执行。我的建议是使用 ASP.NEt 路由引擎使用的类似代码。
There is no doubt that you need to create a custom extension method because ASP.NET routing engine uses this code to decide whether a file exist,
You will not able to decide whether the request is static in Application_BeginRequest using context.handler because Routing Module may change the handler and this module always execute after Application_BeginRequest. My suggestion is to use the similar code which ASP.NEt routing engine uses.