我有一個Global.asx文件,需要做自定義驗證,審計和分析的東西。這是必需的,因爲它支持基於SAML的SSO系統,並且需要覆蓋正常的.Net身份驗證(它不支持SAML或混合身份驗證)在Application_BeginRequest期間檢查靜態文件嗎?
我不想將它解僱爲靜態文件,例如.js
,.css
,.png
等
在Cassini/WebDev和IIS7中它有。
我想要的是一些簡單的檢查,如(不存在,不幸),以識別靜態文件。
我意識到這會寫起來相當簡單,但它感覺像是必須已經存在的東西 - 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配置?
我意識到我可以編寫自己的實現(就像我在問題中說的那樣),但感覺就像重新發明輪子。 IIS和.Net已經知道這是靜態文件請求,所以應該有一種現有的方法來做到這一點。 – Keith