how to avoid session timeout in web.config

后端 未结 6 734
萌比男神i
萌比男神i 2020-12-18 00:21

What should I write in web config file in asp.net so that my session time is extended. and please tell me the exact location where should I place the code in web config

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 00:44

    If you are trying to stop the session from timeing out all the time you can do this rather than increasing the session timeout.

    KeepAlive.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="KeepAlive.aspx.cs" Inherits="Pages.KeepAlive" %>
    
    
    <%@ OutputCache Location="None" VaryByParam="None" %>
    
    
        
    
    
        
    KEEP ALIVE

    Keep Alive.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Pages
    {
        /// 
        /// Page to keep the session alive
        /// 
        public partial class KeepAlive : System.Web.UI.Page
        {
            //- EVENTS ------------------------------------------------------------------------------------------------------------------
    
            #region Events
    
            /// 
            /// Page Load
            /// 
            /// object
            /// args
            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    //Add refresh header to refresh the page 60 seconds before session timeout
                    Response.AddHeader("Refresh", Convert.ToString((Session.Timeout * 60) - 60));
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
            #endregion Events
    
            //---------------------------------------------------------------------------------------------------------------------------
        }
    }
    

    Then in your master page create an iFrame that refreshes to keep the session alive

    
    

提交回复
热议问题