Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在应用程序启动请求期间检查静态文件?_Asp.net_Asp.net Mvc_Global Asax_Static Files - Fatal编程技术网

Asp.net 在应用程序启动请求期间检查静态文件?

Asp.net 在应用程序启动请求期间检查静态文件?,asp.net,asp.net-mvc,global-asax,static-files,Asp.net,Asp.net Mvc,Global Asax,Static Files,我有一个Global.asx文件,需要进行自定义身份验证、审核和分析。这是必需的,因为它支持基于SAML的SSO系统,并且需要覆盖正常的.Net身份验证(不支持SAML或混合身份验证) 我不想为静态文件启动它,例如.js,.css,.png,等等 在Cassini/WebDev和IIS7中,确实如此 我想要的是一些简单的检查,比如this.Request.IsStaticFile(很遗憾,它不存在)来识别静态文件 我意识到这将是相当简单的编写,但它感觉像是一些必须已经存在的东西-IIS已经应用了

我有一个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);
}
///保留我们视为静态的所有扩展
静态HashSet allowedExtensions=新HashSet(StringComparer.OrdinalIgnoreCase)
{
.js、.css、.png、。。。
};
///这是对静态文件的请求吗?
///要扩展的HTTP请求实例。
///如果请求的是磁盘上的静态文件,则为True,否则为false。
公共静态bool IsStaticFile(此HttpRequest请求)
{
字符串fileOnDisk=request.PhysicalPath;
if(string.IsNullOrEmpty(fileOnDisk))
{
返回false;
}
字符串扩展名=Path.GetExtension(fileOnDisk);
返回allowedExtensions.Contains(扩展名);
}
这很有效,速度也很快,但感觉非常笨拙。特别是,如果我们添加了没有想到的新静态文件,那么依赖扩展将很容易出错


有没有更好的方法不更改IIS配置?

您可以检查哪个处理程序正在处理请求

在IIS6中,只有.net文件,例如aspx被映射到一个处理程序

在集成管道的IIS7中,所有内容都通过.net路由,这通常是件好事。不过,不同的处理程序仍然处理不同的文件类型。特别是,我认为staticfilehandler是您需要检查的。
httpcontext.handler
属性应该允许您找出它

您可以创建一个扩展方法来添加IsStatic方法


西蒙

有几种选择:

  • 为不需要任何身份验证且包含静态文件的路径添加
    授权
    并拒绝无
  • 您正在使用集成管道。在IIS 7上关闭它

    • 毫无疑问,您需要创建自定义扩展名方法,因为ASP.NET路由引擎使用此代码来确定文件是否存在

      if (!this.RouteExistingFiles)
      {
          string appRelativeCurrentExecutionFilePath = httpContext.Request.AppRelativeCurrentExecutionFilePath;
           if (((appRelativeCurrentExecutionFilePath != "~/") && (this._vpp != null)) && (this._vpp.FileExists(appRelativeCurrentExecutionFilePath) || this._vpp.DirectoryExists(appRelativeCurrentExecutionFilePath)))
           {
                return null;
             }
      }
      

      您将无法使用context.handler确定应用程序_BeginRequest中的请求是否是静态的,因为路由模块可能会更改处理程序,并且此模块始终在应用程序_BeginRequest之后执行。我的建议是使用ASP.NEt路由引擎使用的类似代码

      为了支持SAML SSO,我必须完全覆盖.Net的授权机制(这就是为什么我首先需要在
      应用程序\u BeginRequest
      中做大量工作),所以选项1不适用。此外,正如我在问题中所说,更改IIS配置不是一个选项-我需要一个代码解决方案。我意识到我可以编写自己的实现(如我在问题中所说),但这感觉就像是重新发明轮子。IIS和.Net已经知道这是静态文件请求,所以应该有一种现有的方法来实现这一点。