简体   繁体   中英

How to increase login session timeout on c# web page in visual studio?

So, here is the thing... When i login using login.aspx, it redirects me to admin.aspx. If leave admin page idle for few minutes and then try to open a web form it gives me an error.

"Object reference not set to an instance of an object."
**Source Error:**
Line 15:     protected void Page_Load(object sender, EventArgs e)
Line 16: { 
Line 17: string s=Session["po"].ToString();
Line 18: 
Line 19: Session["y"]=".";

这是Screeshot图像

Now when i login again pages work fine. And again after few minutes i got this error whenever i try to open a page from admin page or try to refresh a page. On the other hand i have few pages and they do not have above session code, they work fine. But only problem is they can be opened without logging in and the ones with this command do not open directly. Whenever i try to open them without logging in i get this error too.

Can you explain me how to fix this issue? Is there any way to increase session timeout? Is this some kind of session logout?

As Jonathon Chase mentioned in the comment you can configure Session timeout in web config Session timeout in ASP.NET

Other solution is to create a service that actually do nothing and make request from the client to this service with some interval of time or just call it once to make the session longer. For example, if your session expires in 30 minutes your interval will be 29 minutes. In this way you can choose in which pages you want to "hold" the session longer or make it infinite.

Service : RetainSession.ashx

using System.Web;
using System.Web.SessionState;

namespace WebApplication
{
    public class RetainSession : IHttpHandler, 
        IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Ok");
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}

Client side:

var interval = 1000 * 60 * 29;
var intervalKey = window.setInterval(function(){
    $.get('/services/RetainSession.ashx?v='+Date.now());
},interval);

To stop interval:

window.clearInterval(intervalKey);

Call once :

var timeout = 1000 * 60 * 29; 
window.setTimeout(function(){
    $.get('/services/RetainSession.ashx?v='+Date.now());
},timeout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM