从web.config重定向到根登录页

本文关键字:登录 重定向 web config | 更新日期: 2023-09-27 17:58:24

会话到期时,我会将用户重定向到登录页面。Login.aspx在根目录中。

我在web.config文件中这样声明了路径。

     <forms name=".FormsAuth" loginUrl="~/Login.aspx"  protection="All" 
slidingExpiration="false" requireSSL="false" >

它适用于所有根.aspx页面。但它不适用于像Reporting'report.aspx这样的子文件夹页面。

那么,如何管理root.aspx页面和子文件夹.aspx页面的重定向页面(Login.aspx)呢?

从web.config重定向到根登录页

检查您的配置。在我看来,您的标签Forms没有正确关闭。如果你有这两个部分,它必须工作:

<authentication mode="Forms">
    <forms name=".FormsAuth" loginUrl="~/Login.aspx" protection="All" slidingExpiration="false" requireSSL="false" />
 </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

你可以在这里找到一个示例应用程序。

默认情况下,名为secure和admin的目录是唯一受保护的目录您需要使用以下命令手动保护您想要的目录:

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

所有子文件夹都必须有自己的web.config文件,以拒绝匿名用户访问,例如:

<configuration>
    <system.web>
      <authorization>
        <deny users="?" />                      <!--Deny all Anonymous (not logged in) users-->
        <allow roles="admin,manager,cservice"/> <!--Permit users in these roles-->
        <deny users="*"/>                       <!--Deny all users-->
      </authorization>
    </system.web>
</configuration>

您也可以尝试使用

系统。网状物安全表单身份验证。重定向到登录页面();

如果您需要使用response.redirect或server.transer 直接重定向,则重定向到配置中设置的登录页面

点击此处查看此页面:http://msdn.microsoft.com/en-us/library/xdt4thhy(v=vs.100).aspx

以下是设置表单身份验证的基本步骤:

web.config:

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH">
    </forms>
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

登录页面:

<script runat="server">
  void Logon_Click(object sender, EventArgs e)
  {
    if ((UserEmail.Text == "jchen@contoso.com") && 
            (UserPass.Text == "37Yj*99Ps"))
      {
          FormsAuthentication.RedirectFromLoginPage 
             (UserEmail.Text, Persist.Checked);
      }
      else
      {
          Msg.Text = "Invalid credentials. Please try again.";
      }
  }
</script>

签出页面:

<script runat="server">
  void Page_Load(object sender, EventArgs e)
  {
    Welcome.Text = "Hello, " + Context.User.Identity.Name;
  }
  void Signout_Click(object sender, EventArgs e)
  {
    FormsAuthentication.SignOut();
    Response.Redirect("Logon.aspx");
  }
</script>