c# - ASP.NET - Javascript 超时警告基于 web.config 中的 sessionState 超时

标签 c# javascript session-state session-timeout

问题:我希望在基于我的 webconfig sessionState TimeOut 属性的 c# 代码背后的 asp.net 页面上创建超时警告消息。

web.config 中的代码:

<configuration>
    <system.web>
        <sessionState timeout="20"></sessionState>
    </system.web>
</configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="EchoSignDocumentService10HttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="6553600" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1638400" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
            <binding name="EchoSignDocumentService10HttpBinding1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>

最佳答案

伪代码:

  • 读取代码隐藏中的超时设置
  • 注册一个ClientScriptblock(setTimeout传递超时时间(20 * 60))
  • 超时时,在页面上显示警告标签

示例代码:

    public void RegisterTimeoutWarning(System.Web.UI.Page Page)
    {
        var timeout = HttpContext.Current.Session.Timeout * 60 * 1000;
        Page.ClientScript.RegisterStartupScript(Page.GetType(), 
                "timeoutWarning", 
                string.Format("setTimeout(function () {{ alert('Session about to expire'); }}, {0});", timeout), true);
    }

当然,您可以通过显示警告弹出窗口甚至确认弹出窗口来改进客户端显示(而不是显示警报),然后您可以使用它来更新 session 。

关于c# - ASP.NET - Javascript 超时警告基于 web.config 中的 sessionState 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7029168/

相关文章:

c# - 将 FLASH 与 ASP.NET 一起使用 - 好还是不好?

c# - 从 await 返回后的意外行为

javascript - 如何在不使用 .then 语法的情况下使 fetch promise 解析?

javascript - JavaScript 中的 MP3 播放器

php - 我如何告诉 sqlmap 检查另一个链接?

asp.net-mvc - Azure、Asp.Net 和 inproc session

c# - 单击富文本框中的链接后如何打开新的浏览器窗口(C#)

c# - C# 中 ValidationError 类的合理模式

javascript - json 日期和时区

python - asp.net 破解 sessionId 使用 python 脚本被暴力破解的可能性