我对我们的内容管理系统提供的文件做了一些类似的事情。如果打开了 http 处理程序,它会检查文件名和路径以查看用户是否有权访问资源。如果用户这样做,它会流式传输文件,否则返回 401 not authorized。
我不明白为什么您不能使用处理程序来跳转到 css 文件的管道、检查主机名并转而输出其他 css 文件(如果适用)。这在带有集成管道的 IIS7 中很简单(您没有指定),但如果您让 .net 处理 css 扩展名,则在 IIS6 中也是可能的。
如果您对这种方法感兴趣,请告诉我,我会追踪一些代码。
编辑 - 这是一些代码
这并不是您想要的,但您可能会得到一些想法。
注意:这是在带有集成管道的 IIS7 中,因此在 IIS6 中您必须进行更改,以便 .net 进程处理 .css 文件。
Public Class FileManagerFileAuthorization
Implements IHttpHandler
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim req As HttpRequest = context.Request
Dim absolutePath As String = req.Path
Dim fileName As String = Path.GetFileName(absolutePath)
Dim physicalPathAndFileName As String = HttpContext.Current.Server.MapPath(absolutePath)
If File.Exists(physicalPathAndFileName) Then
' roles that the user is part of. If the user is not authenticated they are part of the public role only
Dim memberRoles As String()
If req.IsAuthenticated = False Then
memberRoles = New String() {ConfigurationManager.AppSettings("PublicRole")}
Else
Dim r As New Roles()
memberRoles = r.GetRolesForUser("")
End If
' check permissions: transmit file or deliver 401
Dim folderVirtualPath As String = Path.GetDirectoryName(absolutePath).Replace("\"c, "/"c)
Dim permissions As FileManager.FolderPermissions = FileManager.GetFolderPermissions(folderVirtualPath, memberRoles)
If permissions And FileManager.FolderPermissions.View Then
context.Response.ContentType = FileManager.GetContentType(fileName)
context.Response.AddHeader("Content-Length", New FileInfo(physicalPathAndFileName).Length.ToString())
context.Response.TransmitFile(physicalPathAndFileName)
Else
' unauthorized
context.Response.StatusCode = 401
End If
Else
' file not found
context.Response.StatusCode = 404
End If
context.Response.End()
End Sub
End Class
还有 web.config - 再一次 - 这是 IIS7,因此您将使用 system.web 部分下的 <httpHandlers/> 部分。我正在查找 Userfiles 目录中的任何文件,但我认为您可以直接指向包含此文件的文件。
<system.webServer>
<handlers>
<add name="FileManagerFileAuthorization" path="Userfiles*" verb="GET" type="FileManagerFileAuthorization" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>
注意:
要允许 .net 处理 IIS 中的非 .net 文件,您必须允许 .net 进程处理这些文件。为此,请打开 IIS 管理器,导航到该网站,然后单击属性。转到“主目录”选项卡,然后单击“配置”。添加通配符映射,然后选择 .net dll。如果您不确定,请从下面的 .ascx 复制链接。
由于是 IIS6,您将无法使用上面的 system.webServer 部分,您需要以旧方式添加 http 处理程序。这个链接解释它:http://msdn.microsoft.com/en-us/library/46c5ddfy.aspx