顯示具有 Session 標籤的文章。 顯示所有文章
顯示具有 Session 標籤的文章。 顯示所有文章

2022年4月25日 星期一

[研究][ASP.NET]Session TimeOut是固定時間 or 閒置時間測試

[研究][ASP.NET]Session TimeOut是固定時間 or 閒置時間測試

2022-04-25

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

參考這篇

[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(一)aspnet_regsql.exe
http://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state.html

資料庫會新增了2個資料表
dbo.ASPStateTempApplications
dbo.ASPStateTempSessions 

其中 dbo.ASPStateTempSessions 的 [Expires] 欄位可看過期時間。

登入後,先檢視一次時間;

放若干秒或分鐘後,隨便動一下滑鼠,再重新 SELECT 該值 ( 要找相同 SessionId 的,在自己開發電腦上測試,只有一個使用者登入,最好認),發現時間變更了,所以是 Session Time Out 是 閒置時間。

注意,[Created], [Expires], [LockDate], [LockDateLocal] 都是 UTC 時間,要加上 8 小時才是台灣時間。

(完)


2022年4月1日 星期五

[研究][ASP.NET]寫在aspx附屬的.cs和單一類別檔案Class1.cs的差異

[研究][ASP.NET]寫在aspx附屬的.cs和單一類別檔案Class1.cs的差異

2022-03-20、2024-03-06

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

錯誤 CS0103 名稱 'Response' 不存在於目前的內容

Response.Redirect("~/Account/Logout.aspx");  

要改寫為

System.Web.HttpContext.Current.Response.Redirect("~/Account/Login.aspx");  

********************************************************************************

錯誤 CS0103 名稱 'Session' 不存在於目前的內容

string account = Session["LoginAccount"].ToString();   

要改寫為

string account= System.Web.HttpContext.Current.Session["LoginAccount"].ToString();   

********************************************************************************

錯誤 CS0103 名稱 'Request' 不存在於目前的內容

string userAgent = Request.ServerVariables["HTTP_USER_AGENT"];   

要改寫為

string userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];   

********************************************************************************

2024-03-06 補

增加是否為 null 判斷

string sessionUserID = System.Web.HttpContext.Current.Session["userID"].ToString();

要改寫為

string sessionUserID = System.Web.HttpContext.Current.Session["userID"] != null ? System.Web.HttpContext.Current.Session["userID"].ToString() : "";

(完)


2022年3月30日 星期三

[研究][ASP.NET]顯示IIS Session Time Out預設值設定時間

[研究][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