Authenticaion失败时重定向到母版页中的页面

本文关键字:母版页 失败 重定向 Authenticaion | 更新日期: 2023-09-27 18:26:57

对于我的web应用程序,我使用Windows身份验证。我在Global.asax页面的Session_Start事件中编写了以下代码;如果登录的用户是有效的,我会将这些详细信息作为存储在会话中

Session["UserDetails"] = "XXXX";

如果用户无效,则

Session["UserDetails"] = null;

然后在我的MasterPage中,我使用以下代码

if(!IsPostBack)
{ 
    if(Session["UserDetails"] == null)
    {
        Response.Redirect("AuthenticationFailure.aspx");
    }
}

但是,它并没有重定向到特定的失败页面。当我在每个aspx页面加载中检查相同的条件时,它似乎运行良好,但页面没有重定向。

这里可能出了什么问题?我应该如何重定向到AuthenticationFailure页面才能使其工作?

Authenticaion失败时重定向到母版页中的页面

由于AuthenticationFailure页面使用的是在用户未通过身份验证时重定向的母版页,因此该页面将永远不会显示,因为它将在到达页面之前进行重定向。

请在母版页中的代码中处理此问题,或者不要将母版页用于身份验证失败页。

您可以使用Request.FilePath变量检查请求的路径,如果路径已位于AuthenticationFailure页面,则可以避免重定向。

if(!IsPostBack)
{ 
    const string FailurePage = "AuthenticationFailure.aspx";
    string page = Request.FilePath.Substring(Request.FilePath.LastIndexOf('/'));
    if(Session["UserDetails"] == null && page != FailurePage)
    {
        Response.Redirect(FailurePage);
    }
}