我在 II6 上有一个 asp.net 2.0 (.net3.5) 网站。 它被设置为使用表单例份验证。 我已将“aspnet_isapi.dll”设置为 IIS 中“通配符应用程序映射”中的条目,因此所有请求都将通过 asp.net。 在 web.config 中,我通过以下设置禁止所有匿名用户:
<system.web>
<authorization>
<deny users="?"/>
</authorization>
...
</system.web>
我想这样做,以便不需要对具有某些扩展名的文件(特别是图像文件,如 .gif、.jpg)进行身份验证。 这些文件位于不同的文件夹中,因此仅授予图像文件夹权限是行不通的。
我修改了自己的 Forms Authentication HttpModule 以忽略所有此类请求并直接返回。但是因为它忽略了这些文件并且没有创建 Principal,所以请求仍然是匿名的并且会出现身份验证错误。
理想情况下,如果我可以执行以下任何一项操作,那就太好了,但它看起来不像是可以完成的操作:
1) 允许我们在 IIS 中设置通配符映射时排除某些文件扩展名。
2) 在我自己的 Forms Authentication HttpModule 中,我可以以某种方式告诉 asp.net 停止该文件的 asp.net 处理管道,或者只是停止对其的身份验证。
3)允许我们在web.config中location标签的path属性中使用通配符,如:
<location path="*.jpg>
<authorization>
<allow users="?"/>
</authorization`>
</system.web`>
我能想到的唯一解决方案是删除 IIS 中的通配符映射并单独添加应用程序扩展映射,因此并非所有请求都由 asp.net 处理。但是,我冒着丢失某些文件类型的风险,更不用说为所有已知文件类型(减去图像文件)单独添加它会很乏味
有什么想法吗?
最佳答案
创建一个通用的认证主体
Public Class BlowByAuthenticatedIdentity
Implements IIdentity
Public ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.AuthenticationType
Get
Return "BlowBy"
End Get
End Property
Public ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticated
Get
Return True
End Get
End Property
Public ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Name
Get
Return String.Empty
End Get
End Property
End Class
在 Global.asax Application_AuthenticateRequest
If Request.Path.ToLower.EndsWith(".jpg") Then
Me.Context.User = BlowByAuthenticatedIdentity
End if
关于asp.net - 如何告诉 ASP.NET Forms 身份验证不对某些文件扩展名进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3367416/