0

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"]=".";

Here is the Screeshot image

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?

  • 1
    Possible duplicate of [Session timeout in ASP.NET](https://stackoverflow.com/questions/648992/session-timeout-in-asp-net) – Jonathon Chase Oct 09 '18 at 17:29
  • You should always check that the Session value you are trying to access is not null before trying to use it, and redirect your user to an appropriate page (probably login) when it is null, rather than throwing a yellow screen or error page. – Jonathon Chase Oct 09 '18 at 17:30
  • Remember, `.ToString()` will throw a null reference exception if the value passed to it is null. That's probably what's going on here. – gunr2171 Oct 09 '18 at 17:30
  • @Jonathan Chase how to check session value? i have no idea about it. Where to look for it? – Jason Bourne Oct 09 '18 at 17:49
  • @Jason In your Page_Load, you check that the session value actually exists like this: `if(Session["po"] == null) Response.Redirect("~/path/to/login.aspx");` – Jonathon Chase Oct 09 '18 at 17:50
  • @Jonathan thanks brother. Its working. Other thing i wanna know how to increase session timeout? I want to set it 1 hr. – Jason Bourne Oct 09 '18 at 18:07

1 Answers1

0

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);