ASP.Net セッションがタイムアウトし (フォーム認証も)、ページにアクセスしようとすると、既定の login.aspx ページに自動的にリダイレクトされます。
ページが読み込まれる前に、これがタイムアウトの状況であるかどうかを判断する必要があります。タイムアウトの場合は、timeout.aspx にリダイレクトします。
以下の記事では、IsNewSession が true で、sessionID Cookie が存在する場合、タイムアウト状態になることを示しています。
ただし、私のテストでは、タイムアウトして再度ログインしようとすると、IsNewSession が true に等しく、sessionId Cookie がまだぶら下がっている (ブラウザー セッション全体で保持されるため) という状況が発生したため、時間を計ったと表示されます。 -再度ログインしようとしているときにもう一度アウトします。
これをすべて行うより良い方法はありますか?
私の「global.asax」ファイルには次のものがあります。
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
// Check if session state is enabled in web.config
if (Context.Session == null) return;
if (Session["user"] == null)
{
if (Session.IsNewSession)
{
HttpCookie sessionCookie = Request.Cookies["ASP.NET_SessionId"];
if ((null != sessionCookie) && !string.IsNullOrEmpty(sessionCookie.Value))
{
/* Session Timeout! */
FormsAuthentication.SignOut(); //just in case not done yet
Session.Abandon();
Response.Redirect("timeout.aspx");
}
else
{
// Cookie didn't exist - must be a brand new login
return;
}
}
else
{
// If there is no session data and the session is not new then it must be the postback of the login screen.
if ((HttpContext.Current.Request.Path.ToLower().LastIndexOf("/login.aspx") >= 0) && (Request.HttpMethod == "POST"))
{
return;
}
}
}
}