【发布时间】:2013-04-02 20:24:49
【问题描述】:
场景
我有一个 ASP.NET 项目,它使用自定义授权/身份验证方法(与使用表单/Windows 身份验证等相比)。在每次 secure 页面加载时,都会执行以下代码:
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsLoggedIn)
{
HttpContext.Current.Response.Redirect("~/Login/", true);
}
}
这段代码主要检查用户是否仍然登录(没有过期的 ASP.NET 会话,没有注销等);如果用户未登录,则会发生Response.Redirect(),将其发送到登录页面。
当用户请求完整页面(通过链接或直接 URL)时,此方案工作得很好。使用异步回发时会出现问题!
我有一个嵌套在 <asp:UpdatePanel> 内的按钮,单击时会导致异步回发。此按钮更新<asp:Label />。例如:
<!-- the button -->
<asp:LinkButton ID="MyButton" CausesValidation="false" Text="My Button" OnClick="MyButton_Click" runat="server" />
<!-- the label -->
<asp:Label ID="MyLabel" runat="server" />
protected void MyButton_Click(Object sender, EventArgs e)
{
MyLabel.Text = DateTime.Now.ToString();
}
问题
当异步回发正在执行并且IsLoggedIn 为假时,请求被重定向到登录页面。现在,ASP.NET 框架需要一个特定的响应(而不是 HTML 页面);因此,抛出以下错误:
问题
我该如何解决这个问题?如何在异步回发期间强制整个页面从代码隐藏重定向到特定地址?
【问题讨论】:
标签: c# asp.net asp.net-ajax postback asp.net-3.5