【问题标题】:How do I allow anonymous users to access an image?如何允许匿名用户访问图像?
【发布时间】:2012-07-26 17:23:32
【问题描述】:

我目前正在开发一个 ASP.NET 网站,该网站使用Forms Authentication 来限制对用户需要注册才能查看的某些文件 (~/resources/restricted/*) 的访问。不幸的是,这具有在网站的不相关部分中禁止背景 (~/common/images/backgrounds/bg_community.jpg) 的额外效果。

我的配置有什么问题/我该如何解决?

~/Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

  <!-- ... --->

  <system.web>

    <!-- ... --->

    <authentication mode="Forms">
      <forms loginUrl="~/users/login.aspx" />
    </authentication>

    <!-- ... --->

  </system.web>

  <!-- ... --->

  <location path="~/resources/restricted">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>

  <!-- ... --->

</configuration>

【问题讨论】:

  • 你不能用 为公共图像目录添加另一个“位置”部分吗? (但也许你已经尝试过了。)
  • 好吧,我真的不想为每个目录都这样做。我正在寻找一种仅排除 ~/resources/restricted 目录并保留其余部分的解决方案。

标签: asp.net web-config forms-authentication


【解决方案1】:

我不确定路径,我认为这就足够了:

path="resources/restricted"

在这种情况下,我喜欢在 /resources/restricted/ 文件夹中创建额外的 web.config 文件。这样您就可以确定您控制的位置。缺点是你有多个配置文件。

<?xml version="1.0"?>
<configuration>
    <system.web>
        <authorization>
            <deny users ="?"/>
        </authorization>
    </system.web>
</configuration>

【讨论】:

  • 对于他所描述的有限情况,这似乎是一种非常容易处理的方法。
猜你喜欢
  • 1970-01-01
  • 2019-11-23
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 2015-08-30
  • 2014-09-02
相关资源
最近更新 更多