我试图限制未经授权的用户访问Utilities.aspx
页面并将他重定向到Default.aspx
页面。
if (authorizedUser.ToLower() != "admin")
{
if (!ClientScript.IsClientScriptBlockRegistered("UnauthorizedUserRedirect"))
{
ClientScript.RegisterStartupScript(this.GetType(), "UnauthorizedUserRedirect", "<script>alert('Unauthorized access!\n\nYou have attempted to access a page that you are not authorized to view.')</script>", true);
Response.Redirect("Default.aspx");
}
}
但是,虽然重定向工作正常,但alert
页面上没有显示。搜索这个问题告诉我 Response.Redirect 在客户端代码完全呈现之前完成了它的操作。
我怎样才能显示alert
之前的Response.Redirect
?
我也尝试了这两种方法,Page_Load
但Default.aspx
都没有奏效。如果设置了某个会话 bariable,则显示警报。
if (Session["unauth"] != null)
{
ClientScript.RegisterStartupScript(this.GetType(), "UnauthorizedUserRedirect", "alert('Unauthorized access!\n\nYou have attempted to access a page that you are not authorized to view.');", true);
}
if (Session["unauth"] != null)
{
form1.Attributes.Add("OnLoad", "javascript:alert('Unauthorized access!\n\nYou have attempted to access a page that you are not authorized to view.');");
}