[研究][C#][ASP.NET][WebForm] Sessionn Time Out 自動登出前倒數計時
Session TimeOut CountDown Timer
2020-11-14
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TimeOutTest1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%--https://docs.microsoft.com/zh-tw/dotnet/api/system.web.ui.timer?view=netframework-4.8--%>
<%--https://docs.microsoft.com/zh-tw/dotnet/api/system.web.sessionstate.httpsessionstate.timeout?view=netframework-4.8--%>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<%--Interval="1000" 是每1秒觸發一次--%>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button_Timer" runat="server" BackColor="LightGreen" Height="40px" Width="100px" Style="border-radius: 8px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TimeOutTest1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/*
* Web.Config 中設定 Session Time Out 時間方法,單位分鐘 (沒設定,則預設值 20 分鐘)
<configuration>
<system.web>
<sessionState timeout="20"></sessionState>
</system.web>
</configuration>
*/
// 取得 Time Out 時間
// 方法1
Configuration conf = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
SessionStateSection section = (SessionStateSection)conf.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes;
// 方法2
System.Int64 timeout2 = System.Web.HttpContext.Current.Session.Timeout;
// 設定 Session Time Out 時間 20 分鐘
if (!ScriptManager1.IsInAsyncPostBack)
{
// 把目前的時間,加上 Session Time Out 設定值,
//Session["timeout"] = DateTime.Now.AddMinutes(20).ToString();
Session["timeout"] = DateTime.Now.AddMinutes(timeout).ToString();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
Button_Timer.Text = ((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).Minutes).ToString()
+ ":" +
((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).Seconds).ToString();
}
}
}
} |
Web.Config
<configuration> <system.web>
<sessionState timeout="20"></sessionState>
</system.web>
</configuration> |
上面的作法是針對個別 .aspx 進行,若要放入主版頁面 Master Page ( Site1.Master 和 Site1.Master.cs 中),會失敗。
重點是使用 Master Page 時候, Site1.Master 中,
if (!ScriptManager1.IsInAsyncPostBack)
要改成
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (!scriptManager.IsInAsyncPostBack)
才行。詳見這篇
[研究][C#][ASP.NET][WebForm] Master Page 的 Session Time Out 自動登出前倒數計時
http://shaurong.blogspot.com/2020/11/caspnetwebform-master-page-sessionn.html
(完)
沒有留言:
張貼留言