简体   繁体   English

重定向到自定义禁止页面在asp.net中不起作用

[英]Redirect to custom forbidden page not working in asp.net

I have a public (no authentication) website, except one page which requires authentication. 我有一个公共(无身份验证)网站,除了需要身份验证的页面之外。 That works fine. 很好

The protected page is within a folder with the following web.config: 受保护的页面位于具有以下web.config的文件夹中:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <authorization>
            <allow roles="Warehouse" />
            <deny users="*" />
        </authorization>

        <customErrors mode="RemoteOnly" defaultRedirect="../WebForbidden.aspx">
            <!--<error statusCode="403" redirect="WebForbidden.aspx" />
            <error statusCode="404" redirect="FileNotFound.htm" />-->
        </customErrors>

    </system.web>
</configuration>

And the master file for that page has the following: 该页面的主文件具有以下内容:

protected void Page_Init(object sender, EventArgs e)
    {
        CheckLogged();
    }

    public bool CheckLogged()
    {
        bool status = false;

        if (Session["Username"] == null)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("../WebForbidden.aspx");
            //FormsAuthentication.RedirectToLoginPage();
        }
        else
        {
            status = true;
        }

        return status;
    }

And I have th WebForbidden.aspx page in the root of the website. 我在网站的根目录中有WebForbidden.aspx页。

Now, everytime I try to enter directly to the protected page (which is forbidden unless authenticated) instead of showing the Forbidden page, an error is shown: The resource cannot be found , of course the website is trying to redirect to the Login page, which not exists. 现在,每次我尝试直接进入受保护的页面(除非经过身份验证即被禁止),而不是显示“禁止的”页面,都会显示一个错误: The resource cannot be found ,当然,网站正在尝试重定向到“登录”页面,不存在。

Any help will be appreciated. 任何帮助将不胜感激。

Try telling forms authentication to redirect to WebForbidden.aspx instead of letting it use the default, which if you don't specify it will look for Login.aspx 尝试让表单身份验证重定向到WebForbidden.aspx而不是让它使用默认值,如果未指定默认值,它将查找Login.aspx

<system.web>
    <authentication mode="Forms">
        <forms loginUrl="WebForbidden.aspx" />
    </authentication>
</system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM