ASP.Net 4.5 表单认证/授权不工作

ASP.Net 4.5 Forms Authentication / Authorization not working

我从一个带有个人帐户的默认 WebForms 项目开始。我有一堆使用数据库连接构建的内容。我想将所有内容限制为经过身份验证的用户,但 default.aspx

除外

我已在我的 SQL 数据库中成功建立身份 table 结构,并且可以 "register" 新用户。这一切都很好。但是,当我将身份验证设置添加到 web.config(见下文)时,一切都中断了。

<system.web>
    <authentication mode="Forms">
        <forms name=".FormsAuth" loginUrl="Login.aspx" protection="All" slidingExpiration="false" requireSSL="false" />
    </authentication>

    <authorization>
        <deny users="?"/>
    </authorization>
</system.web>
<location path="Default.aspx">
    <system.web>
        <authorization>
            <allow users="*"/>
        </authorization>
    </system.web>
</location>

我希望这能让我查看我的 Default.aspx 页面并在我离开它时重定向。相反,我尝试重定向到 \account\login 并失败并显示此消息。

HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.

ReturnURL 很大而且似乎在重复自己。我试着四处寻找一个从头开始的例子,但没有找到一个可行的例子。这个应该很简单。

http://localhost:58573/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252FAccount%25252525252525252525252525252525252FLogin%25252525252525252525252525252525253FReturnUrl%25252525252525252525252525252525253D%2525252525252525252525252525252525252FAccount%2525252525252525252525252525252525252FLogin%2525252525252525252525252525252525253FReturnUrl%2525252525252525252525252525252525253D%252525252525252525252525252525252525252FDefault

我想通了。我不得不从 web.config:

中删除一般的 "deny all anonymous" 语句
 <!--<authorization>
        <deny users="?"/>
      </authorization>-->

...我试图用它来限制除登录页面之外的所有内容。 我将所有内容移动到几个子文件夹中,然后使用位置标签和相同的拒绝用户声明将它们调出。

<location path="System">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
    <location path="Reports">
      <system.web>
        <authorization>
          <deny users="?"/>
        </authorization>
      </system.web>
  </location>

此时它似乎可以正常工作 "properly",如果未通过身份验证,现在会将用户重定向到 login.aspx。

\account\login.aspx 由于 web.config 而被拒绝。 ...

<authorization>
    <deny users="?"/>
</authorization>

当你重定向到登录页面时,因为禁止匿名访问,你又被重定向到登录页面,导致递归。 你可以在账号folder.The中创建web.config内容是这样的:

<system.web>
<authorization>
    <allow users="*"/>
</authorization>