[研究][ASP.NET]顯示IIS Session Time Out預設值設定時間
2022-03-20
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
目前為了安全性,各 Web Server 預設都有 Session TimeOut 機制,並且有各自的預設時間,依據Web Server 種類、版本可能都有所不同。
ASP.NET WebForm 網站 Web.config 可以設定 Session TimeOut 值 (不設也會有預設值)。
Session Time Out 在設定後是一個定值,Remaining Session Time 剩餘會話時間是指 Session 在 Time Out 前剩下的時間,是動態值。
Web.Config,例如設定為20分鐘。
<configuration>
<system.web>
<sessionState timeout="20"></sessionState>
</system.web>
</configuration> |
如果不知道,可以用程式測試看看
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1.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>
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Label ID="Label2" runat="server"></asp:Label><br />
</div>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Configuration;
using System.Web.Configuration;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Configuration conf = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
SessionStateSection section = (SessionStateSection)conf.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes;
Label1.Text = timeout.ToString();
System.Int64 timeout2 = System.Web.HttpContext.Current.Session.Timeout; // The time-out period, in minutes
Label2.Text = timeout2.ToString();
}
}
}
|
測試出來 Windows Server 2019 + IIS Web Server + ASP.NET WebForm Web Application 網站的 Session TimeOut 預設值為 20 分鐘。
(完)
相關
sessionState 項目 (ASP.NET 設定結構描述) | Microsoft Docs
https://docs.microsoft.com/zh-tw/previous-versions/dotnet/netframework-3.0/h6bb9cz9(v=vs.85)
對談 (電腦科學) - 維基百科,自由的百科全書https://zh.wikipedia.org/wiki/%E4%BC%9A%E8%AF%9D_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6)
HttpSessionState.Timeout 屬性 (System.Web.SessionState) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.web.sessionstate.httpsessionstate.timeout?view=netframework-4.8
沒有留言:
張貼留言