如何使用表单身份验证重定向用户到特定页面

4
我希望配置应用程序,防止用户在未登录的情况下直接访问应用程序中的任何页面,但任何用户都可以访问网站首页。
但是当我运行主页、登录页面或任何网站页面时,我收到以下错误提示:“请求的页面无法访问,因为页面的相关配置数据无效。”
我找不出我的错误所在。我已经发布了我的web.config文件,请查看它。告诉我我犯了哪个错误,并提供解决方案。 web.config

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

  <authentication mode="Forms">
    
    <forms loginUrl="/Registration/LoginPage.aspx">
      
    </forms>
    
  </authentication>
  

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>
  
  <location path="FIRST PAGE">
      <system.web>
        <authorization>
          <allow users="*"/>
          
        </authorization>
      </system.web>
    </location>
  
  <location path="Registration">
      <system.web>
        <authorization>
          <allow users="?"/>
          
        </authorization>
      </system.web>
    </location>
  
  
    <location path="AdminHome">
      <system.web>
        <authorization>
          <allow users="admin"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>
  
  <location path="Student">
      <system.web>
        <authorization>
          <allow roles="Student"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>
  
<location path="Teacher">
      <system.web>
        <authorization>
          <allow roles="Teacher"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>

  <appSettings>

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
    
  </appSettings>
  

</configuration>

错误

在此输入图像描述

在此输入图像描述

网站主页位于“FIRST PAGE”文件夹下,登录和注册页面位于“Registration”文件夹下。

1个回答

1

您的配置文件中的<authentication>应该位于<system.web>部分内。

MSDN authentication Element

只需编辑您的web.config文件:

<system.web>
   <authentication mode="Forms">
       <forms loginUrl="/Registration/LoginPage.aspx">
       </forms>
   </authentication>
   <compilation debug="true" targetFramework="4.5.2" />
   <httpRuntime targetFramework="4.5.2" />
</system.web>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接