2020年11月17日 星期二

[研究][C#][ASP.NET][WebForm][JavaScript]顯示 Web.Config 設定 Session Out Time 時間

[研究][C#][ASP.NET][WebForm][JavaScript]顯示 Web.Config 設定 Session Out Time 時間

2020-11-17

載入網頁時,顯示 Web.Config 設定 Session Time 時間。

參考下面修改,但有些 Code 不能用

Alert Session Time out in ASP.NET
https://www.codeproject.com/Articles/227382/Alert-Session-Time-out-in-ASP-Net

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.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>
    <script language="javascript" type="text/javascript">
        //https://www.codeproject.com/Articles/227382/Alert-Session-Time-out-in-ASP-Net
        //This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings
        <%--var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>"; /*fail,沒有彈出對話盒*/--%>
        <%--var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationManager.AppSettings["SessionWarning"].ToString()%>";  /*fail,沒有彈出對話盒*/--%>
        //var sessionTimeoutWarning = 1;  /*fail,沒有彈出對話盒*/
        var sessionTimeoutWarning = 0;    /* OK */
        var sessionTimeout = "<%= Session.Timeout %>";

        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

        function SessionWarning() {
            var message = "Your session will expire in another " +
                (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) +
                " mins! Please Save the data before the session expires";
            alert(message);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>


Default.aspx.cs (不用修改)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Web.Config
<?xml version="1.0" encoding="utf-8"?>
<!--
  如需如何設定 ASP.NET 應用程式的詳細資訊,請前往
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
	<system.web>
		<compilation debug="true" targetFramework="4.7.2"/>
		<httpRuntime targetFramework="4.7.2"/>
	</system.web>
	<appSettings>
		<!--Session Time Out 前1分鐘警告-->
		<add key="SessionWarning" value="1" />
	</appSettings>
	<system.codedom>
		<compilers>
			<compiler language="c#;cs;csharp" extension=".cs"
			  type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
			  warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
			<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
			  type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
			  warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
		</compilers>
	</system.codedom>
</configuration>

這邊有討論此種作法問題

https://www.itdaan.com/tw/e65f06f8610f33f3662810e8dd7b4261

(完)

沒有留言:

張貼留言