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

时间:2016-05-05 05:53:50

标签: asp.net webforms 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>
&#13;
&#13;
&#13;

错误

enter image description here

enter image description here

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

1 个答案:

答案 0 :(得分: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>