Asp.net HTTP模块中的HttpContext.RewritePath()生成403禁止
我想使用以下HTTP模块将请求重写到另一个文件夹的文件:Asp.net HTTP模块中的HttpContext.RewritePath()生成403禁止,asp.net,iis,url-rewriting,httpmodule,Asp.net,Iis,Url Rewriting,Httpmodule,我想使用以下HTTP模块将请求重写到另一个文件夹的文件: public class MyHttpModule : IHttpModule { public void Init(HttpApplication application) { application.BeginRequest += (sender, e) => { var httpApplication = (HttpApplication)
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (sender, e) =>
{
var httpApplication = (HttpApplication)sender;
var httpContext = httpApplication.Context;
if (httpContext.Request.Url.ToString().Contains("styles/style.css"))
{
httpContext.RewritePath("~/some/other/folder/styles/style.css");
}
};
}
public void Dispose() { }
}
代码似乎按预期工作(即在RewritePath()
Request.Url
包含重写的Url之后),但当我尝试打开styles/style.css时,收到403.2禁止错误
当我在浏览器中打开重写的URL(取自Request.URL
,在RewritePath()
调用之后)时,我正确地看到了CSS文件(即,我打开类似于styles/style.CSS的内容,并获得了某些/other/folder/styles/style.CSS下的内容)
失败的请求跟踪还表明我的模块进行了正确的重写。这是与错误对应的跟踪条目:
82. -NOTIFY_MODULE_END
ModuleName ManagedPipelineHandler
Notification MAP_REQUEST_HANDLER
fIsPostNotificationEvent false
NotificationStatus NOTIFICATION_CONTINUE
83. -MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName IIS Web Core
Notification MAP_REQUEST_HANDLER
HttpStatus 403
HttpReason Forbidden
HttpSubStatus 2
ErrorCode Access is denied. (0x80070005)
84. -SECURITY_DENIED_BY_ACCESS_FLAGS
CurrentFlags 512
NeededFlags 1
据我所知,它需要AccessRead(1)权限,但得到了AccessScript(512)(请参阅)。这让我感到困惑,因为我猜后者的权限级别比前者高
更改有关文件文件夹的文件系统权限似乎没有效果(即使将完全控制权授予所有人也无济于事)。应用程序池使用我的用户(管理员帐户)作为其身份(但将其保留在AppPoolIdentity上也没有帮助)
该文件的文件夹有一个Web.config,其中包含以下内容(如果重要):
<handlers accessPolicy="Script,Read">
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
</handlers>
使用IIS URL重写模块执行相同的重写也可以工作
更新:
我注意到,在原始的、不存在的路径(即~/styles/style.css)下创建一个物理文件夹,只包含一个带有上述处理程序声明的Web.config(加上与system.webServer等相关的常见仪式),会使错误消失。当然,这对我来说不是一个解决方案,但至少它给出了一些提示
更新2:
我用一个空的ASP.NET应用程序做了一些测试。只在根Web.config中设置模块,而不将Web.config添加到Styles文件夹是有效的
将其添加到根Web.config(这是原始应用程序的Web.config中的内容)会导致错误。哈!
<handlers accessPolicy="Script">
<clear />
</handlers>
将accessPolicy修改为“脚本,读取”。解决了这个问题
有人能解释一下吗?您可以尝试在“some”文件夹[~/some/other/folder/styles/style.css]中创建一个web.config,并删除其子文件夹中的所有web.config文件。在only web.config中,您可以尝试以下操作:
<handlers accessPolicy="Script,Read">
<!--
iis7 - for any request to a file exists on disk, return it via native http module.
accessPolicy 'Script' is to allow for a managed 404 page.
-->
<add name="StaticFileHandler" path="*" verb="*" modules="StaticFileModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
</handlers>
您能提供一个不使用Orchard的复制程序吗?我可以与其他人共享该解决方案。谢谢,但这会产生同样的效果。“some”文件夹现在只包含此路径的子文件夹,以及带有Web.config的单个styles.css(尝试了原始的styles.css,尝试了您的styles.css),因此假定没有其他干扰。