許可されていないユーザーがページにアクセスするのを制限し、そのユーザーを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ですか?
また、これら 2 つのアプローチをPage_Loadof で試しましたがDefault.aspx、どちらも機能しませんでした。特定のセッションバリアブルが設定されている場合、アラートを表示します。
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.');");
}