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

How to redirect a user to a Specific Page with Forms Authentication

我想配置应用程序并防止用户在未登录的情况下直接进入应用程序中的任何页面,但任何用户都可以访问网站主页。

但是当我 运行 主页、登录页面或网站的任何页面时,出现此错误:- The requested page cannot be accessed because the related configuration data for the page is invalid.

我找不到我哪里出错了。我已经发布了我的 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

配置的 <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>